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/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/object/type-registry-helper.h>
28 #include <dali/public-api/scripting/scripting.h>
29 #include <dali/integration-api/debug.h>
31 namespace //Unnamed namespace
36 //Todo: make these properties instead of constants
37 const unsigned int GAUSSIAN_BLUR_DEFAULT_NUM_SAMPLES = 11;
38 const unsigned int GAUSSIAN_BLUR_NUM_SAMPLES_INCREMENTATION = 10;
39 const float GAUSSIAN_BLUR_BELL_CURVE_WIDTH = 4.5f;
40 const float GAUSSIAN_BLUR_BELL_CURVE_WIDTH_INCREMENTATION = 5.f;
41 const Pixel::Format GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT = Pixel::RGB888;
42 const float GAUSSIAN_BLUR_DOWNSAMPLE_WIDTH_SCALE = 0.5f;
43 const float GAUSSIAN_BLUR_DOWNSAMPLE_HEIGHT_SCALE = 0.5f;
46 * 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.
48 struct ActorOpacityConstraint
50 ActorOpacityConstraint(int totalImageNum, int currentImageIdx)
52 float rangeLength = 1.f / static_cast<float>( totalImageNum );
53 float index = static_cast<float>( currentImageIdx );
54 mRange = Vector2( index*rangeLength, (index+1.f)*rangeLength );
57 float operator()( float current, const PropertyInput& blurProperty )
59 float blurStrength = blurProperty.GetFloat();
60 if(blurStrength <= mRange.x)
64 else if(blurStrength > mRange.y)
70 return (mRange.y - blurStrength)/(mRange.y-mRange.x);
91 const unsigned int DEFAULT_BLUR_LEVEL(5u); ///< The default blur level when creating SuperBlurView from the type registry
95 return Toolkit::SuperBlurView::New( DEFAULT_BLUR_LEVEL );
98 // Setup properties, signals and actions using the type-registry.
99 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::SuperBlurView, Toolkit::Control, Create )
101 DALI_PROPERTY_REGISTRATION( SuperBlurView, "image", MAP, IMAGE )
103 DALI_TYPE_REGISTRATION_END()
105 } // unnamed namespace
107 SuperBlurView::SuperBlurView( unsigned int blurLevels )
108 : Control( CONTROL_BEHAVIOUR_NONE ),
109 mBlurLevels( blurLevels ),
110 mBlurStrengthPropertyIndex(Property::INVALID_INDEX),
111 mResourcesCleared( true ),
112 mTargetSize( Vector2::ZERO )
114 DALI_ASSERT_ALWAYS( mBlurLevels > 0 && " Minimal blur level is one, otherwise no blur is needed" );
115 mGaussianBlurView.assign( blurLevels, NULL );
116 mBlurredImage.assign( blurLevels, FrameBufferImage() );
117 mImageActors.assign( blurLevels + 1, ImageActor() );
120 SuperBlurView::~SuperBlurView()
124 Toolkit::SuperBlurView SuperBlurView::New( unsigned int blurLevels )
126 //Create the implementation
127 IntrusivePtr<SuperBlurView> superBlurView( new SuperBlurView( blurLevels ) );
129 //Pass ownership to CustomActor via derived handle
130 Toolkit::SuperBlurView handle( *superBlurView );
132 // Second-phase init of the implementation
133 // This can only be done after the CustomActor connection has been made...
134 superBlurView->Initialize();
139 void SuperBlurView::OnInitialize()
141 mBlurStrengthPropertyIndex = Self().RegisterProperty( "blur-strength",0.f );
143 DALI_ASSERT_ALWAYS( mImageActors.size() == mBlurLevels+1 && "must synchronize the ImageActor group if blur levels got changed " );
144 for(unsigned int i=0; i<=mBlurLevels;i++)
146 mImageActors[i] = ImageActor::New( );
147 mImageActors[i].SetParentOrigin( ParentOrigin::CENTER );
148 mImageActors[i].SetZ(-static_cast<float>(i)*0.01f);
149 mImageActors[i].SetColorMode( USE_OWN_MULTIPLY_PARENT_ALPHA );
150 Self().Add( mImageActors[i] );
153 for(unsigned int i=0; i < mBlurLevels; i++)
155 mImageActors[i].ApplyConstraint( Constraint::New<float>( Actor::Property::COLOR_ALPHA, ParentSource( mBlurStrengthPropertyIndex ), ActorOpacityConstraint(mBlurLevels, i) ) );
158 Self().SetSize(Stage::GetCurrent().GetSize());
161 void SuperBlurView::SetImage(Image inputImage)
163 DALI_ASSERT_ALWAYS( mImageActors.size() == mBlurLevels+1 && "must synchronize the ImageActor group if blur levels got changed " );
164 DALI_ASSERT_ALWAYS( mBlurredImage.size() == mBlurLevels && "must synchronize the blurred image group if blur levels got changed " );
168 mImageActors[0].SetImage( inputImage );
170 for(unsigned int i=1; i<=mBlurLevels;i++)
172 mImageActors[i].SetImage( mBlurredImage[i-1] );
175 BlurImage( 0, inputImage);
176 for(unsigned int i=1; i<mBlurLevels;i++)
178 BlurImage( i, mBlurredImage[i-1]);
181 mResourcesCleared = false;
184 Property::Index SuperBlurView::GetBlurStrengthPropertyIndex() const
186 return mBlurStrengthPropertyIndex;
189 void SuperBlurView::SetBlurStrength( float blurStrength )
191 Self().SetProperty(mBlurStrengthPropertyIndex, blurStrength);
194 float SuperBlurView::GetCurrentBlurStrength() const
197 (Self().GetProperty( mBlurStrengthPropertyIndex )).Get(blurStrength);
202 Toolkit::SuperBlurView::SuperBlurViewSignal& SuperBlurView::BlurFinishedSignal()
204 return mBlurFinishedSignal;
207 Image SuperBlurView::GetBlurredImage( unsigned int level )
209 DALI_ASSERT_ALWAYS( level>0 && level<=mBlurLevels );
210 return mBlurredImage[level-1];
213 void SuperBlurView::BlurImage( unsigned int idx, Image image )
215 DALI_ASSERT_ALWAYS( mGaussianBlurView.size()>idx );
216 mGaussianBlurView[idx] = Toolkit::GaussianBlurView::New( GAUSSIAN_BLUR_DEFAULT_NUM_SAMPLES+GAUSSIAN_BLUR_NUM_SAMPLES_INCREMENTATION*idx,
217 GAUSSIAN_BLUR_BELL_CURVE_WIDTH + GAUSSIAN_BLUR_BELL_CURVE_WIDTH_INCREMENTATION*static_cast<float>(idx),
218 GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT,
219 GAUSSIAN_BLUR_DOWNSAMPLE_WIDTH_SCALE, GAUSSIAN_BLUR_DOWNSAMPLE_HEIGHT_SCALE, true );
220 mGaussianBlurView[idx].SetParentOrigin(ParentOrigin::CENTER);
221 mGaussianBlurView[idx].SetSize(mTargetSize);
222 mGaussianBlurView[idx].SetUserImageAndOutputRenderTarget( image, mBlurredImage[idx] );
223 if( idx == mBlurLevels-1 )
225 mGaussianBlurView[idx].FinishedSignal().Connect( this, &SuperBlurView::OnBlurViewFinished );
227 Stage::GetCurrent().Add( mGaussianBlurView[idx] );
228 mGaussianBlurView[idx].ActivateOnce();
231 void SuperBlurView::OnBlurViewFinished( Toolkit::GaussianBlurView blurView )
234 Toolkit::SuperBlurView handle( GetOwner() );
235 mBlurFinishedSignal.Emit( handle );
238 void SuperBlurView::ClearBlurResource()
240 if( !mResourcesCleared )
242 DALI_ASSERT_ALWAYS( mGaussianBlurView.size() == mBlurLevels && "must synchronize the GaussianBlurView group if blur levels got changed " );
243 for(unsigned int i=0; i<mBlurLevels;i++)
245 Stage::GetCurrent().Remove( mGaussianBlurView[i] );
246 mGaussianBlurView[i].Deactivate();
247 mGaussianBlurView[i].Reset();
249 mResourcesCleared = true;
253 void SuperBlurView::OnRelayout( const Vector2& size, ActorSizeContainer& container )
255 unsigned int numChildren = Self().GetChildCount();
257 for( unsigned int i=0; i<numChildren; ++i )
259 Self().GetChildAt(i).SetSize(size);
263 void SuperBlurView::OnControlSizeSet( const Vector3& targetSize )
265 if( mTargetSize != Vector2(targetSize) )
267 mTargetSize = Vector2(targetSize);
269 for(unsigned int i=0; i<mBlurLevels;i++)
271 float exponent = static_cast<float>(i+1);
272 mBlurredImage[i] = FrameBufferImage::New( mTargetSize.width/std::pow(2.f,exponent) , mTargetSize.height/std::pow(2.f,exponent),
273 GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT, Dali::Image::NEVER );
278 void SuperBlurView::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
280 Toolkit::SuperBlurView superBlurView = Toolkit::SuperBlurView::DownCast( Dali::BaseHandle( object ) );
284 SuperBlurView& superBlurViewImpl( GetImpl( superBlurView ) );
286 if( propertyIndex == Toolkit::SuperBlurView::Property::IMAGE )
288 Dali::Image image = Scripting::NewImage( value );
291 superBlurViewImpl.SetImage( image );
295 DALI_LOG_ERROR( "Cannot create image from property value\n" );
301 Property::Value SuperBlurView::GetProperty( BaseObject* object, Property::Index propertyIndex )
303 Property::Value value;
305 Toolkit::SuperBlurView pushButton = Toolkit::SuperBlurView::DownCast( Dali::BaseHandle( object ) );
309 SuperBlurView& superBlurViewImpl( GetImpl( pushButton ) );
311 if( propertyIndex == Toolkit::SuperBlurView::Property::IMAGE )
314 if( !superBlurViewImpl.mImageActors.empty() && superBlurViewImpl.mImageActors[0] )
316 Scripting::CreatePropertyMap( superBlurViewImpl.mImageActors[0], map );
318 value = Property::Value( map );
325 } // namespace Internal
327 } // namespace Toolkit