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 <dali/public-api/common/stage.h>
21 #include <dali/public-api/object/type-registry.h>
22 #include <dali/public-api/scripting/scripting.h>
23 #include <dali/integration-api/debug.h>
26 #include "super-blur-view-impl.h"
28 namespace //unnamed namespace
33 //Todo: make these properties instead of constants
34 const unsigned int GAUSSIAN_BLUR_DEFAULT_NUM_SAMPLES = 11;
35 const unsigned int GAUSSIAN_BLUR_NUM_SAMPLES_INCREMENTATION = 10;
36 const float GAUSSIAN_BLUR_BELL_CURVE_WIDTH = 4.5f;
37 const float GAUSSIAN_BLUR_BELL_CURVE_WIDTH_INCREMENTATION = 5.f;
38 const Pixel::Format GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT = Pixel::RGB888;
39 const float GAUSSIAN_BLUR_DOWNSAMPLE_WIDTH_SCALE = 0.5f;
40 const float GAUSSIAN_BLUR_DOWNSAMPLE_HEIGHT_SCALE = 0.5f;
43 * 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.
45 struct ActorOpacityConstraint
47 ActorOpacityConstraint(int totalImageNum, int currentImageIdx)
49 float rangeLength = 1.f / static_cast<float>( totalImageNum );
50 float index = static_cast<float>( currentImageIdx );
51 mRange = Vector2( index*rangeLength, (index+1.f)*rangeLength );
54 float operator()( float current, const PropertyInput& blurProperty )
56 float blurStrength = blurProperty.GetFloat();
57 if(blurStrength <= mRange.x)
61 else if(blurStrength > mRange.y)
67 return (mRange.y - blurStrength)/(mRange.y-mRange.x);
82 const Property::Index SuperBlurView::PROPERTY_IMAGE( Internal::SuperBlurView::SUPER_BLUR_VIEW_PROPERTY_START_INDEX );
89 const unsigned int DEFAULT_BLUR_LEVEL(5u); ///< The default blur level when creating SuperBlurView from the type registry
93 return Toolkit::SuperBlurView::New( DEFAULT_BLUR_LEVEL );
97 TypeRegistration typeRegistration( typeid(Toolkit::SuperBlurView), typeid(Toolkit::Control), Create );
99 PropertyRegistration property1( typeRegistration, "image", Toolkit::SuperBlurView::PROPERTY_IMAGE, Property::MAP, &SuperBlurView::SetProperty, &SuperBlurView::GetProperty );
101 } // unnamed namespace
103 SuperBlurView::SuperBlurView( unsigned int blurLevels )
104 : Control( CONTROL_BEHAVIOUR_NONE ),
105 mBlurLevels( blurLevels ),
106 mBlurStrengthPropertyIndex(Property::INVALID_INDEX),
107 mResourcesCleared( true ),
108 mTargetSize( Vector2::ZERO )
110 DALI_ASSERT_ALWAYS( mBlurLevels > 0 && " Minimal blur level is one, otherwise no blur is needed" );
111 mGaussianBlurView.assign( blurLevels, NULL );
112 mBlurredImage.assign( blurLevels, FrameBufferImage() );
113 mImageActors.assign( blurLevels + 1, ImageActor() );
116 SuperBlurView::~SuperBlurView()
120 Toolkit::SuperBlurView SuperBlurView::New( unsigned int blurLevels )
122 //Create the implementation
123 IntrusivePtr<SuperBlurView> superBlurView( new SuperBlurView( blurLevels ) );
125 //Pass ownership to CustomActor via derived handle
126 Toolkit::SuperBlurView handle( *superBlurView );
128 // Second-phase init of the implementation
129 // This can only be done after the CustomActor connection has been made...
130 superBlurView->Initialize();
135 void SuperBlurView::OnInitialize()
137 mBlurStrengthPropertyIndex = Self().RegisterProperty( "blur-strength",0.f );
139 DALI_ASSERT_ALWAYS( mImageActors.size() == mBlurLevels+1 && "must synchronize the ImageActor group if blur levels got changed " );
140 for(unsigned int i=0; i<=mBlurLevels;i++)
142 mImageActors[i] = ImageActor::New( );
143 mImageActors[i].SetParentOrigin( ParentOrigin::CENTER );
144 mImageActors[i].SetZ(-static_cast<float>(i)*0.01f);
145 mImageActors[i].SetColorMode( USE_OWN_MULTIPLY_PARENT_ALPHA );
146 Self().Add( mImageActors[i] );
149 for(unsigned int i=0; i < mBlurLevels; i++)
151 mImageActors[i].ApplyConstraint( Constraint::New<float>( Actor::COLOR_ALPHA, ParentSource( mBlurStrengthPropertyIndex ), ActorOpacityConstraint(mBlurLevels, i) ) );
154 Self().SetSize(Stage::GetCurrent().GetSize());
157 void SuperBlurView::SetImage(Image inputImage)
159 DALI_ASSERT_ALWAYS( mImageActors.size() == mBlurLevels+1 && "must synchronize the ImageActor group if blur levels got changed " );
160 DALI_ASSERT_ALWAYS( mBlurredImage.size() == mBlurLevels && "must synchronize the blurred image group if blur levels got changed " );
164 mImageActors[0].SetImage( inputImage );
166 for(unsigned int i=1; i<=mBlurLevels;i++)
168 mImageActors[i].SetImage( mBlurredImage[i-1] );
171 BlurImage( 0, inputImage);
172 for(unsigned int i=1; i<mBlurLevels;i++)
174 BlurImage( i, mBlurredImage[i-1]);
177 mResourcesCleared = false;
180 Property::Index SuperBlurView::GetBlurStrengthPropertyIndex() const
182 return mBlurStrengthPropertyIndex;
185 void SuperBlurView::SetBlurStrength( float blurStrength )
187 Self().SetProperty(mBlurStrengthPropertyIndex, blurStrength);
190 float SuperBlurView::GetCurrentBlurStrength() const
193 (Self().GetProperty( mBlurStrengthPropertyIndex )).Get(blurStrength);
198 Toolkit::SuperBlurView::SuperBlurViewSignal& SuperBlurView::BlurFinishedSignal()
200 return mBlurFinishedSignal;
203 Image SuperBlurView::GetBlurredImage( unsigned int level )
205 DALI_ASSERT_ALWAYS( level>0 && level<=mBlurLevels );
206 return mBlurredImage[level-1];
209 void SuperBlurView::BlurImage( unsigned int idx, Image image )
211 DALI_ASSERT_ALWAYS( mGaussianBlurView.size()>idx );
212 mGaussianBlurView[idx] = Toolkit::GaussianBlurView::New( GAUSSIAN_BLUR_DEFAULT_NUM_SAMPLES+GAUSSIAN_BLUR_NUM_SAMPLES_INCREMENTATION*idx,
213 GAUSSIAN_BLUR_BELL_CURVE_WIDTH + GAUSSIAN_BLUR_BELL_CURVE_WIDTH_INCREMENTATION*static_cast<float>(idx),
214 GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT,
215 GAUSSIAN_BLUR_DOWNSAMPLE_WIDTH_SCALE, GAUSSIAN_BLUR_DOWNSAMPLE_HEIGHT_SCALE, true );
216 mGaussianBlurView[idx].SetParentOrigin(ParentOrigin::CENTER);
217 mGaussianBlurView[idx].SetSize(mTargetSize);
218 mGaussianBlurView[idx].SetUserImageAndOutputRenderTarget( image, mBlurredImage[idx] );
219 if( idx == mBlurLevels-1 )
221 mGaussianBlurView[idx].FinishedSignal().Connect( this, &SuperBlurView::OnBlurViewFinished );
223 Stage::GetCurrent().Add( mGaussianBlurView[idx] );
224 mGaussianBlurView[idx].ActivateOnce();
227 void SuperBlurView::OnBlurViewFinished( Toolkit::GaussianBlurView blurView )
230 Toolkit::SuperBlurView handle( GetOwner() );
231 mBlurFinishedSignal.Emit( handle );
234 void SuperBlurView::ClearBlurResource()
236 if( !mResourcesCleared )
238 DALI_ASSERT_ALWAYS( mGaussianBlurView.size() == mBlurLevels && "must synchronize the GaussianBlurView group if blur levels got changed " );
239 for(unsigned int i=0; i<mBlurLevels;i++)
241 Stage::GetCurrent().Remove( mGaussianBlurView[i] );
242 mGaussianBlurView[i].Deactivate();
243 mGaussianBlurView[i].Reset();
245 mResourcesCleared = true;
249 void SuperBlurView::OnRelayout( const Vector2& size, ActorSizeContainer& container )
251 unsigned int numChildren = Self().GetChildCount();
253 for( unsigned int i=0; i<numChildren; ++i )
255 Self().GetChildAt(i).SetSize(size);
259 void SuperBlurView::OnControlSizeSet( const Vector3& targetSize )
261 if( mTargetSize != Vector2(targetSize) )
263 mTargetSize = Vector2(targetSize);
265 for(unsigned int i=0; i<mBlurLevels;i++)
267 float exponent = static_cast<float>(i+1);
268 mBlurredImage[i] = FrameBufferImage::New( mTargetSize.width/std::pow(2.f,exponent) , mTargetSize.height/std::pow(2.f,exponent),
269 GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT, Dali::Image::Never );
274 void SuperBlurView::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
276 Toolkit::SuperBlurView superBlurView = Toolkit::SuperBlurView::DownCast( Dali::BaseHandle( object ) );
280 SuperBlurView& superBlurViewImpl( GetImpl( superBlurView ) );
282 switch ( propertyIndex )
284 case Toolkit::SuperBlurView::PROPERTY_IMAGE:
286 Dali::Image image = Scripting::NewImage( value );
289 superBlurViewImpl.SetImage( image );
293 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 switch ( propertyIndex )
313 case Toolkit::SuperBlurView::PROPERTY_IMAGE:
316 if ( !superBlurViewImpl.mImageActors.empty() && superBlurViewImpl.mImageActors[0] )
318 Scripting::CreatePropertyMap( superBlurViewImpl.mImageActors[0], map );
320 value = Property::Value( map );
329 } // namespace Internal
331 } // namespace Toolkit