2 * Copyright (c) 2017 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/public-api/image-loader/sync-image-loader.h>
34 #include <dali-toolkit/devel-api/controls/control-devel.h>
35 #include <dali-toolkit/internal/controls/control/control-renderers.h>
36 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
37 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
39 namespace //Unnamed namespace
44 //Todo: make these properties instead of constants
45 const unsigned int GAUSSIAN_BLUR_DEFAULT_NUM_SAMPLES = 11;
46 const unsigned int GAUSSIAN_BLUR_NUM_SAMPLES_INCREMENTATION = 10;
47 const float GAUSSIAN_BLUR_BELL_CURVE_WIDTH = 4.5f;
48 const float GAUSSIAN_BLUR_BELL_CURVE_WIDTH_INCREMENTATION = 5.f;
49 const Pixel::Format GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT = Pixel::RGBA8888;
50 const float GAUSSIAN_BLUR_DOWNSAMPLE_WIDTH_SCALE = 0.5f;
51 const float GAUSSIAN_BLUR_DOWNSAMPLE_HEIGHT_SCALE = 0.5f;
53 const char* ALPHA_UNIFORM_NAME( "uAlpha" );
54 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
55 varying mediump vec2 vTexCoord;\n
56 uniform sampler2D sTexture;\n
57 uniform lowp vec4 uColor;\n
58 uniform lowp float uAlpha;\n
62 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
63 gl_FragColor.a *= uAlpha;
68 * 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.
70 struct ActorOpacityConstraint
72 ActorOpacityConstraint(int totalImageNum, int currentImageIdx)
74 float rangeLength = 1.f / static_cast<float>( totalImageNum );
75 float index = static_cast<float>( currentImageIdx );
76 mRange = Vector2( index*rangeLength, (index+1.f)*rangeLength );
79 void operator()( float& current, const PropertyInputContainer& inputs )
81 float blurStrength = inputs[0]->GetFloat();
82 if(blurStrength < mRange.x)
86 else if(blurStrength > mRange.y)
92 current = ( blurStrength - mRange.x) / ( mRange.y - mRange.x );
113 const unsigned int DEFAULT_BLUR_LEVEL(5u); ///< The default blur level when creating SuperBlurView from the type registry
117 return Toolkit::SuperBlurView::New( DEFAULT_BLUR_LEVEL );
120 // Setup properties, signals and actions using the type-registry.
121 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::SuperBlurView, Toolkit::Control, Create )
123 DALI_PROPERTY_REGISTRATION( Toolkit, SuperBlurView, "imageUrl", STRING, IMAGE_URL )
125 DALI_TYPE_REGISTRATION_END()
127 } // unnamed namespace
129 SuperBlurView::SuperBlurView( unsigned int blurLevels )
130 : Control( ControlBehaviour( DISABLE_SIZE_NEGOTIATION | DISABLE_STYLE_CHANGE_SIGNALS ) ),
131 mTargetSize( Vector2::ZERO ),
132 mBlurStrengthPropertyIndex(Property::INVALID_INDEX),
133 mBlurLevels( blurLevels ),
134 mResourcesCleared( true )
136 DALI_ASSERT_ALWAYS( mBlurLevels > 0 && " Minimal blur level is one, otherwise no blur is needed" );
137 mGaussianBlurView.assign( blurLevels, Toolkit::GaussianBlurView() );
138 mBlurredImage.assign( blurLevels, FrameBuffer() );
139 mRenderers.assign( blurLevels+1, Dali::Renderer() );
142 SuperBlurView::~SuperBlurView()
146 Toolkit::SuperBlurView SuperBlurView::New( unsigned int blurLevels )
148 //Create the implementation
149 IntrusivePtr<SuperBlurView> superBlurView( new SuperBlurView( blurLevels ) );
151 //Pass ownership to CustomActor via derived handle
152 Toolkit::SuperBlurView handle( *superBlurView );
154 // Second-phase init of the implementation
155 // This can only be done after the CustomActor connection has been made...
156 superBlurView->Initialize();
161 void SuperBlurView::OnInitialize()
163 Actor self( Self() );
165 mBlurStrengthPropertyIndex = self.RegisterProperty( "blurStrength", 0.f );
168 void SuperBlurView::SetTexture( Texture texture )
170 mInputTexture = texture;
172 if( mTargetSize == Vector2::ZERO )
179 Actor self( Self() );
181 BlurTexture( 0, mInputTexture );
182 SetRendererTexture( mRenderers[0], texture );
185 for(; i<mBlurLevels; i++)
187 BlurTexture( i, mBlurredImage[i-1].GetColorTexture() );
188 SetRendererTexture( mRenderers[i], mBlurredImage[i-1] );
191 SetRendererTexture( mRenderers[i], mBlurredImage[i-1] );
193 mResourcesCleared = false;
196 Property::Index SuperBlurView::GetBlurStrengthPropertyIndex() const
198 return mBlurStrengthPropertyIndex;
201 void SuperBlurView::SetBlurStrength( float blurStrength )
203 Self().SetProperty(mBlurStrengthPropertyIndex, blurStrength);
206 float SuperBlurView::GetCurrentBlurStrength() const
209 (Self().GetProperty( mBlurStrengthPropertyIndex )).Get(blurStrength);
214 Toolkit::SuperBlurView::SuperBlurViewSignal& SuperBlurView::BlurFinishedSignal()
216 return mBlurFinishedSignal;
219 Texture SuperBlurView::GetBlurredTexture( unsigned int level )
221 DALI_ASSERT_ALWAYS( level>0 && level<=mBlurLevels );
223 FrameBuffer frameBuffer = mBlurredImage[level-1];
225 return frameBuffer.GetColorTexture();
228 void SuperBlurView::BlurTexture( unsigned int idx, Texture texture )
230 DALI_ASSERT_ALWAYS( mGaussianBlurView.size()>idx );
231 mGaussianBlurView[idx] = Toolkit::GaussianBlurView::New( GAUSSIAN_BLUR_DEFAULT_NUM_SAMPLES+GAUSSIAN_BLUR_NUM_SAMPLES_INCREMENTATION*idx,
232 GAUSSIAN_BLUR_BELL_CURVE_WIDTH + GAUSSIAN_BLUR_BELL_CURVE_WIDTH_INCREMENTATION*static_cast<float>(idx),
233 GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT,
234 GAUSSIAN_BLUR_DOWNSAMPLE_WIDTH_SCALE, GAUSSIAN_BLUR_DOWNSAMPLE_HEIGHT_SCALE, true );
235 mGaussianBlurView[idx].SetParentOrigin(ParentOrigin::CENTER);
236 mGaussianBlurView[idx].SetSize(mTargetSize);
237 Stage::GetCurrent().Add( mGaussianBlurView[idx] );
239 mGaussianBlurView[idx].SetUserImageAndOutputRenderTarget( texture, mBlurredImage[idx] );
241 mGaussianBlurView[idx].ActivateOnce();
242 if( idx == mBlurLevels-1 )
244 mGaussianBlurView[idx].FinishedSignal().Connect( this, &SuperBlurView::OnBlurViewFinished );
248 void SuperBlurView::OnBlurViewFinished( Toolkit::GaussianBlurView blurView )
251 Toolkit::SuperBlurView handle( GetOwner() );
252 mBlurFinishedSignal.Emit( handle );
255 void SuperBlurView::ClearBlurResource()
257 if( !mResourcesCleared )
259 DALI_ASSERT_ALWAYS( mGaussianBlurView.size() == mBlurLevels && "must synchronize the GaussianBlurView group if blur levels got changed " );
260 for(unsigned int i=0; i<mBlurLevels;i++)
262 Stage::GetCurrent().Remove( mGaussianBlurView[i] );
263 mGaussianBlurView[i].Deactivate();
265 mResourcesCleared = true;
269 void SuperBlurView::OnSizeSet( const Vector3& targetSize )
271 if( mTargetSize != Vector2(targetSize) )
273 mTargetSize = Vector2(targetSize);
276 for( unsigned int i = 1; i <= mBlurLevels; i++ )
278 float exponent = static_cast<float>(i);
280 unsigned int width = mTargetSize.width/std::pow(2.f,exponent);
281 unsigned int height = mTargetSize.height/std::pow(2.f,exponent);
283 mBlurredImage[i-1] = FrameBuffer::New( width, height, FrameBuffer::Attachment::NONE );
284 Texture texture = Texture::New( TextureType::TEXTURE_2D, GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT, unsigned(width), unsigned(height) );
285 mBlurredImage[i-1].AttachColorTexture( texture );
290 SetTexture( mInputTexture );
294 Control::OnSizeSet( targetSize );
297 void SuperBlurView::OnStageConnection( int depth )
299 if( mTargetSize == Vector2::ZERO )
304 // Exception to the rule, chaining up first ensures visuals have SetOnStage called to create their renderers
305 Control::OnStageConnection( depth );
309 for(unsigned int i=0; i<mBlurLevels+1;i++)
311 mRenderers[i] = CreateRenderer( BASIC_VERTEX_SOURCE, FRAGMENT_SHADER );
312 mRenderers[i].SetProperty( Dali::Renderer::Property::DEPTH_INDEX, (int)i );
313 self.AddRenderer( mRenderers[i] );
317 Renderer renderer = mRenderers[i];
318 Property::Index index = renderer.RegisterProperty( ALPHA_UNIFORM_NAME, 0.f );
319 Constraint constraint = Constraint::New<float>( renderer, index, ActorOpacityConstraint(mBlurLevels, i-1) );
320 constraint.AddSource( Source( self, mBlurStrengthPropertyIndex ) );
327 SetRendererTexture( mRenderers[0], mInputTexture );
329 for(; i<mBlurLevels; i++)
331 SetRendererTexture( mRenderers[i], mBlurredImage[i-1] );
333 SetRendererTexture( mRenderers[i], mBlurredImage[i-1] );
337 void SuperBlurView::OnStageDisconnection()
339 for(unsigned int i=0; i<mBlurLevels+1;i++)
341 Self().RemoveRenderer( mRenderers[i] );
342 mRenderers[i].Reset();
345 Control::OnStageDisconnection();
348 Vector3 SuperBlurView::GetNaturalSize()
352 return Vector3( mInputTexture.GetWidth(), mInputTexture.GetHeight(), 0.f );
354 return Vector3::ZERO;
357 void SuperBlurView::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
359 Toolkit::SuperBlurView superBlurView = Toolkit::SuperBlurView::DownCast( Dali::BaseHandle( object ) );
363 SuperBlurView& superBlurViewImpl( GetImpl( superBlurView ) );
365 if( propertyIndex == Toolkit::SuperBlurView::Property::IMAGE_URL )
367 value.Get( superBlurViewImpl.mUrl );
369 PixelData pixels = SyncImageLoader::Load( superBlurViewImpl.mUrl );
373 Texture texture = Texture::New( TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight() );
374 texture.Upload( pixels, 0, 0, 0, 0, pixels.GetWidth(), pixels.GetHeight() );
376 superBlurViewImpl.SetTexture( texture );
380 DALI_LOG_ERROR( "Cannot create image from property value\n" );
386 Property::Value SuperBlurView::GetProperty( BaseObject* object, Property::Index propertyIndex )
388 Property::Value value;
390 Toolkit::SuperBlurView blurView = Toolkit::SuperBlurView::DownCast( Dali::BaseHandle( object ) );
394 SuperBlurView& superBlurViewImpl( GetImpl( blurView ) );
396 if( propertyIndex == Toolkit::SuperBlurView::Property::IMAGE_URL )
398 value = superBlurViewImpl.mUrl;
405 } // namespace Internal
407 } // namespace Toolkit