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.
20 #include <integration-api/debug.h>
23 #include "super-blur-view-impl.h"
25 namespace //unnamed namespace
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;
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.
42 struct ActorOpacityConstraint
44 ActorOpacityConstraint(int totalImageNum, int currentImageIdx)
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 );
51 float operator()( float current, const PropertyInput& blurProperty )
53 float blurStrength = blurProperty.GetFloat();
54 if(blurStrength <= mRange.x)
58 else if(blurStrength > mRange.y)
64 return (mRange.y - blurStrength)/(mRange.y-mRange.x);
79 const Property::Index SuperBlurView::PROPERTY_IMAGE( Internal::SuperBlurView::SUPER_BLUR_VIEW_PROPERTY_START_INDEX );
86 const unsigned int DEFAULT_BLUR_LEVEL(5u); ///< The default blur level when creating SuperBlurView from the type registry
90 return Toolkit::SuperBlurView::New( DEFAULT_BLUR_LEVEL );
94 TypeRegistration typeRegistration( typeid(Toolkit::SuperBlurView), typeid(Toolkit::Control), Create );
96 PropertyRegistration property1( typeRegistration, "image", Toolkit::SuperBlurView::PROPERTY_IMAGE, Property::MAP, &SuperBlurView::SetProperty, &SuperBlurView::GetProperty );
98 } // unnamed namespace
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 )
107 DALI_ASSERT_ALWAYS( mBlurLevels > 0 && " Minimal blur level is one, otherwise no blur is needed" );
108 mGaussianBlurView.assign( blurLevels, NULL );
109 mBlurredImage.assign( blurLevels, FrameBufferImage() );
110 mImageActors.assign( blurLevels + 1, ImageActor() );
113 SuperBlurView::~SuperBlurView()
117 Toolkit::SuperBlurView SuperBlurView::New( unsigned int blurLevels )
119 //Create the implementation
120 IntrusivePtr<SuperBlurView> superBlurView( new SuperBlurView( blurLevels ) );
122 //Pass ownership to CustomActor via derived handle
123 Toolkit::SuperBlurView handle( *superBlurView );
125 // Second-phase init of the implementation
126 // This can only be done after the CustomActor connection has been made...
127 superBlurView->Initialize();
132 void SuperBlurView::OnInitialize()
134 mBlurStrengthPropertyIndex = Self().RegisterProperty( "blur-strength",0.f );
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++)
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] );
146 for(unsigned int i=0; i < mBlurLevels; i++)
148 mImageActors[i].ApplyConstraint( Constraint::New<float>( Actor::COLOR_ALPHA, ParentSource( mBlurStrengthPropertyIndex ), ActorOpacityConstraint(mBlurLevels, i) ) );
151 Self().SetSize(Stage::GetCurrent().GetSize());
154 void SuperBlurView::SetImage(Image inputImage)
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 " );
161 mImageActors[0].SetImage( inputImage );
163 for(unsigned int i=1; i<=mBlurLevels;i++)
165 mImageActors[i].SetImage( mBlurredImage[i-1] );
168 BlurImage( 0, inputImage);
169 for(unsigned int i=1; i<mBlurLevels;i++)
171 BlurImage( i, mBlurredImage[i-1]);
174 mResourcesCleared = false;
177 Property::Index SuperBlurView::GetBlurStrengthPropertyIndex() const
179 return mBlurStrengthPropertyIndex;
182 void SuperBlurView::SetBlurStrength( float blurStrength )
184 Self().SetProperty(mBlurStrengthPropertyIndex, blurStrength);
187 float SuperBlurView::GetCurrentBlurStrength() const
190 (Self().GetProperty( mBlurStrengthPropertyIndex )).Get(blurStrength);
195 Toolkit::SuperBlurView::SuperBlurViewSignal& SuperBlurView::BlurFinishedSignal()
197 return mBlurFinishedSignal;
200 Image SuperBlurView::GetBlurredImage( unsigned int level )
202 DALI_ASSERT_ALWAYS( level>0 && level<=mBlurLevels );
203 return mBlurredImage[level-1];
206 void SuperBlurView::BlurImage( unsigned int idx, Image image )
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 )
218 mGaussianBlurView[idx].FinishedSignal().Connect( this, &SuperBlurView::OnBlurViewFinished );
220 Stage::GetCurrent().Add( mGaussianBlurView[idx] );
221 mGaussianBlurView[idx].ActivateOnce();
224 void SuperBlurView::OnBlurViewFinished( Toolkit::GaussianBlurView blurView )
227 Toolkit::SuperBlurView handle( GetOwner() );
228 mBlurFinishedSignal.Emit( handle );
231 void SuperBlurView::ClearBlurResource()
233 if( !mResourcesCleared )
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++)
238 Stage::GetCurrent().Remove( mGaussianBlurView[i] );
239 mGaussianBlurView[i].Deactivate();
240 mGaussianBlurView[i].Reset();
242 mResourcesCleared = true;
246 void SuperBlurView::OnRelaidOut( Vector2 size, ActorSizeContainer& container )
248 unsigned int numChildren = Self().GetChildCount();
250 for( unsigned int i=0; i<numChildren; ++i )
252 Self().GetChildAt(i).SetSize(size);
256 void SuperBlurView::OnControlSizeSet( const Vector3& targetSize )
258 if( mTargetSize != Vector2(targetSize) )
260 mTargetSize = Vector2(targetSize);
262 for(unsigned int i=0; i<mBlurLevels;i++)
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 );
271 void SuperBlurView::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
273 Toolkit::SuperBlurView superBlurView = Toolkit::SuperBlurView::DownCast( Dali::BaseHandle( object ) );
277 SuperBlurView& superBlurViewImpl( GetImpl( superBlurView ) );
279 switch ( propertyIndex )
281 case Toolkit::SuperBlurView::PROPERTY_IMAGE:
283 Dali::Image image = Scripting::NewImage( value );
286 superBlurViewImpl.SetImage( image );
290 DALI_LOG_ERROR( "Cannot create image from property value\n" );
298 Property::Value SuperBlurView::GetProperty( BaseObject* object, Property::Index propertyIndex )
300 Property::Value value;
302 Toolkit::SuperBlurView pushButton = Toolkit::SuperBlurView::DownCast( Dali::BaseHandle( object ) );
306 SuperBlurView& superBlurViewImpl( GetImpl( pushButton ) );
308 switch ( propertyIndex )
310 case Toolkit::SuperBlurView::PROPERTY_IMAGE:
313 if ( !superBlurViewImpl.mImageActors.empty() && superBlurViewImpl.mImageActors[0] )
315 Scripting::CreatePropertyMap( superBlurViewImpl.mImageActors[0], map );
317 value = Property::Value( map );
326 } // namespace Internal
328 } // namespace Toolkit