2 * Copyright (c) 2016 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/property-map.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/rendering/renderer.h>
29 #include <dali/devel-api/scripting/scripting.h>
30 #include <dali/integration-api/debug.h>
33 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
35 namespace //Unnamed namespace
40 //Todo: make these properties instead of constants
41 const unsigned int GAUSSIAN_BLUR_DEFAULT_NUM_SAMPLES = 11;
42 const unsigned int GAUSSIAN_BLUR_NUM_SAMPLES_INCREMENTATION = 10;
43 const float GAUSSIAN_BLUR_BELL_CURVE_WIDTH = 4.5f;
44 const float GAUSSIAN_BLUR_BELL_CURVE_WIDTH_INCREMENTATION = 5.f;
45 const Pixel::Format GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT = Pixel::RGBA8888;
46 const float GAUSSIAN_BLUR_DOWNSAMPLE_WIDTH_SCALE = 0.5f;
47 const float GAUSSIAN_BLUR_DOWNSAMPLE_HEIGHT_SCALE = 0.5f;
49 const char* ALPHA_UNIFORM_NAME( "uAlpha" );
50 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
51 varying mediump vec2 vTexCoord;\n
52 uniform sampler2D sTexture;\n
53 uniform lowp vec4 uColor;\n
54 uniform lowp float uAlpha;\n
58 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
59 gl_FragColor.a *= uAlpha;
64 * 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.
66 struct ActorOpacityConstraint
68 ActorOpacityConstraint(int totalImageNum, int currentImageIdx)
70 float rangeLength = 1.f / static_cast<float>( totalImageNum );
71 float index = static_cast<float>( currentImageIdx );
72 mRange = Vector2( index*rangeLength, (index+1.f)*rangeLength );
75 void operator()( float& current, const PropertyInputContainer& inputs )
77 float blurStrength = inputs[0]->GetFloat();
78 if(blurStrength < mRange.x)
82 else if(blurStrength > mRange.y)
88 current = ( blurStrength - mRange.x) / ( mRange.y - mRange.x );
109 const unsigned int DEFAULT_BLUR_LEVEL(5u); ///< The default blur level when creating SuperBlurView from the type registry
113 return Toolkit::SuperBlurView::New( DEFAULT_BLUR_LEVEL );
116 // Setup properties, signals and actions using the type-registry.
117 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::SuperBlurView, Toolkit::Control, Create )
119 DALI_PROPERTY_REGISTRATION( Toolkit, SuperBlurView, "image", MAP, IMAGE )
121 DALI_TYPE_REGISTRATION_END()
123 } // unnamed namespace
125 SuperBlurView::SuperBlurView( unsigned int blurLevels )
126 : Control( ControlBehaviour( DISABLE_SIZE_NEGOTIATION ) ),
127 mTargetSize( Vector2::ZERO ),
128 mBlurStrengthPropertyIndex(Property::INVALID_INDEX),
129 mBlurLevels( blurLevels ),
130 mResourcesCleared( true )
132 DALI_ASSERT_ALWAYS( mBlurLevels > 0 && " Minimal blur level is one, otherwise no blur is needed" );
133 mGaussianBlurView.assign( blurLevels, Toolkit::GaussianBlurView() );
134 mBlurredImage.assign( blurLevels, FrameBufferImage() );
135 mVisuals.assign( blurLevels+1, Toolkit::Visual::Base() );
138 SuperBlurView::~SuperBlurView()
142 Toolkit::SuperBlurView SuperBlurView::New( unsigned int blurLevels )
144 //Create the implementation
145 IntrusivePtr<SuperBlurView> superBlurView( new SuperBlurView( blurLevels ) );
147 //Pass ownership to CustomActor via derived handle
148 Toolkit::SuperBlurView handle( *superBlurView );
150 // Second-phase init of the implementation
151 // This can only be done after the CustomActor connection has been made...
152 superBlurView->Initialize();
157 void SuperBlurView::OnInitialize()
159 mBlurStrengthPropertyIndex = Self().RegisterProperty( "blurStrength", 0.f );
162 void SuperBlurView::SetImage(Image inputImage)
164 if( mTargetSize == Vector2::ZERO || mInputImage == inputImage)
171 mInputImage = inputImage;
172 Actor self( Self() );
173 InitializeVisual( self, mVisuals[0], mInputImage );
174 mVisuals[0].SetDepthIndex(0);
175 SetShaderEffect( mVisuals[0] );
178 mVisuals[0].SetOnStage( self );
181 BlurImage( 0, inputImage);
182 for(unsigned int i=1; i<mBlurLevels;i++)
184 BlurImage( i, mBlurredImage[i-1]);
187 mResourcesCleared = false;
190 Image SuperBlurView::GetImage()
195 Property::Index SuperBlurView::GetBlurStrengthPropertyIndex() const
197 return mBlurStrengthPropertyIndex;
200 void SuperBlurView::SetBlurStrength( float blurStrength )
202 Self().SetProperty(mBlurStrengthPropertyIndex, blurStrength);
205 float SuperBlurView::GetCurrentBlurStrength() const
208 (Self().GetProperty( mBlurStrengthPropertyIndex )).Get(blurStrength);
213 Toolkit::SuperBlurView::SuperBlurViewSignal& SuperBlurView::BlurFinishedSignal()
215 return mBlurFinishedSignal;
218 Image SuperBlurView::GetBlurredImage( unsigned int level )
220 DALI_ASSERT_ALWAYS( level>0 && level<=mBlurLevels );
221 return mBlurredImage[level-1];
224 void SuperBlurView::BlurImage( unsigned int idx, Image image )
226 DALI_ASSERT_ALWAYS( mGaussianBlurView.size()>idx );
227 mGaussianBlurView[idx] = Toolkit::GaussianBlurView::New( GAUSSIAN_BLUR_DEFAULT_NUM_SAMPLES+GAUSSIAN_BLUR_NUM_SAMPLES_INCREMENTATION*idx,
228 GAUSSIAN_BLUR_BELL_CURVE_WIDTH + GAUSSIAN_BLUR_BELL_CURVE_WIDTH_INCREMENTATION*static_cast<float>(idx),
229 GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT,
230 GAUSSIAN_BLUR_DOWNSAMPLE_WIDTH_SCALE, GAUSSIAN_BLUR_DOWNSAMPLE_HEIGHT_SCALE, true );
231 mGaussianBlurView[idx].SetParentOrigin(ParentOrigin::CENTER);
232 mGaussianBlurView[idx].SetSize(mTargetSize);
233 Stage::GetCurrent().Add( mGaussianBlurView[idx] );
234 mGaussianBlurView[idx].SetUserImageAndOutputRenderTarget( image, mBlurredImage[idx] );
235 mGaussianBlurView[idx].ActivateOnce();
236 if( idx == mBlurLevels-1 )
238 mGaussianBlurView[idx].FinishedSignal().Connect( this, &SuperBlurView::OnBlurViewFinished );
242 void SuperBlurView::OnBlurViewFinished( Toolkit::GaussianBlurView blurView )
245 Toolkit::SuperBlurView handle( GetOwner() );
246 mBlurFinishedSignal.Emit( handle );
249 void SuperBlurView::ClearBlurResource()
251 if( !mResourcesCleared )
253 DALI_ASSERT_ALWAYS( mGaussianBlurView.size() == mBlurLevels && "must synchronize the GaussianBlurView group if blur levels got changed " );
254 for(unsigned int i=0; i<mBlurLevels;i++)
256 Stage::GetCurrent().Remove( mGaussianBlurView[i] );
257 mGaussianBlurView[i].Deactivate();
259 mResourcesCleared = true;
262 void SuperBlurView::SetShaderEffect( Toolkit::Visual::Base& visual )
264 Property::Map shaderMap;
265 std::stringstream verterShaderString;
266 shaderMap[ "fragmentShader" ] = FRAGMENT_SHADER;
268 Internal::Visual::Base& rendererImpl = Toolkit::GetImplementation( visual );
269 rendererImpl.SetCustomShader( shaderMap );
272 void SuperBlurView::OnSizeSet( const Vector3& targetSize )
274 if( mTargetSize != Vector2(targetSize) )
276 mTargetSize = Vector2(targetSize);
279 for( unsigned int i = 1; i <= mBlurLevels; i++ )
281 float exponent = static_cast<float>(i);
282 mBlurredImage[i-1] = FrameBufferImage::New( mTargetSize.width/std::pow(2.f,exponent) , mTargetSize.height/std::pow(2.f,exponent),
283 GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT );
284 InitializeVisual( self, mVisuals[i], mBlurredImage[i - 1] );
285 mVisuals[ i ].SetDepthIndex( i );
286 SetShaderEffect( mVisuals[ i ] );
291 SetImage( mInputImage );
296 for( unsigned int i = 1; i <= mBlurLevels; i++ )
298 mVisuals[i].SetOnStage( self );
304 void SuperBlurView::OnStageConnection( int depth )
306 Control::OnStageConnection( depth );
308 if( mTargetSize == Vector2::ZERO )
316 mVisuals[0].SetOnStage( self );
318 for(unsigned int i=1; i<=mBlurLevels;i++)
322 mVisuals[i].SetOnStage( self );
325 Renderer renderer = self.GetRendererAt( i );
326 Property::Index index = renderer.RegisterProperty( ALPHA_UNIFORM_NAME, 0.f );
327 Constraint constraint = Constraint::New<float>( renderer, index, ActorOpacityConstraint(mBlurLevels, i-1) );
328 constraint.AddSource( Source( self, mBlurStrengthPropertyIndex ) );
333 void SuperBlurView::OnStageDisconnection( )
335 if( mTargetSize == Vector2::ZERO )
341 for(unsigned int i=0; i<mBlurLevels+1;i++)
343 mVisuals[i].SetOffStage( self );
346 Control::OnStageDisconnection();
349 Vector3 SuperBlurView::GetNaturalSize()
353 return Vector3( mInputImage.GetWidth(), mInputImage.GetHeight(), 0.f );
355 return Vector3::ZERO;
358 void SuperBlurView::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
360 Toolkit::SuperBlurView superBlurView = Toolkit::SuperBlurView::DownCast( Dali::BaseHandle( object ) );
364 SuperBlurView& superBlurViewImpl( GetImpl( superBlurView ) );
366 if( propertyIndex == Toolkit::SuperBlurView::Property::IMAGE )
368 Dali::Image image = Scripting::NewImage( value );
371 superBlurViewImpl.SetImage( image );
375 DALI_LOG_ERROR( "Cannot create image from property value\n" );
381 Property::Value SuperBlurView::GetProperty( BaseObject* object, Property::Index propertyIndex )
383 Property::Value value;
385 Toolkit::SuperBlurView blurView = Toolkit::SuperBlurView::DownCast( Dali::BaseHandle( object ) );
389 SuperBlurView& superBlurViewImpl( GetImpl( blurView ) );
391 if( propertyIndex == Toolkit::SuperBlurView::Property::IMAGE )
394 Image inputImage = superBlurViewImpl.GetImage();
397 Scripting::CreatePropertyMap( inputImage, map );
399 value = Property::Value( map );
406 } // namespace Internal
408 } // namespace Toolkit