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/visuals/visual-base-impl.h>
36 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
38 namespace //Unnamed namespace
43 //Todo: make these properties instead of constants
44 const unsigned int GAUSSIAN_BLUR_DEFAULT_NUM_SAMPLES = 11;
45 const unsigned int GAUSSIAN_BLUR_NUM_SAMPLES_INCREMENTATION = 10;
46 const float GAUSSIAN_BLUR_BELL_CURVE_WIDTH = 4.5f;
47 const float GAUSSIAN_BLUR_BELL_CURVE_WIDTH_INCREMENTATION = 5.f;
48 const Pixel::Format GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT = Pixel::RGBA8888;
49 const float GAUSSIAN_BLUR_DOWNSAMPLE_WIDTH_SCALE = 0.5f;
50 const float GAUSSIAN_BLUR_DOWNSAMPLE_HEIGHT_SCALE = 0.5f;
52 const char* ALPHA_UNIFORM_NAME( "uAlpha" );
53 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
54 varying mediump vec2 vTexCoord;\n
55 uniform sampler2D sTexture;\n
56 uniform lowp vec4 uColor;\n
57 uniform lowp float uAlpha;\n
61 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
62 gl_FragColor.a *= uAlpha;
67 * 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.
69 struct ActorOpacityConstraint
71 ActorOpacityConstraint(int totalImageNum, int currentImageIdx)
73 float rangeLength = 1.f / static_cast<float>( totalImageNum );
74 float index = static_cast<float>( currentImageIdx );
75 mRange = Vector2( index*rangeLength, (index+1.f)*rangeLength );
78 void operator()( float& current, const PropertyInputContainer& inputs )
80 float blurStrength = inputs[0]->GetFloat();
81 if(blurStrength < mRange.x)
85 else if(blurStrength > mRange.y)
91 current = ( blurStrength - mRange.x) / ( mRange.y - mRange.x );
98 #define DALI_COMPOSE_SHADER(STR) #STR
100 const char * const BASIC_VERTEX_SOURCE = DALI_COMPOSE_SHADER(
101 precision mediump float;\n
102 attribute mediump vec2 aPosition;\n
103 attribute mediump vec2 aTexture;\n
104 varying mediump vec2 vTexCoord;\n
105 uniform mediump mat4 uMvpMatrix;\n
106 uniform mediump vec3 uSize;\n
110 mediump vec4 vertexPosition = vec4(aPosition * uSize.xy, 0.0, 1.0);\n
111 vTexCoord = aTexture;\n
112 gl_Position = uMvpMatrix * vertexPosition;\n
116 const char * const BASIC_FRAGMENT_SOURCE = DALI_COMPOSE_SHADER(
117 precision mediump float;\n
118 varying mediump vec2 vTexCoord;\n
119 uniform sampler2D sTexture;\n
120 uniform vec4 uColor;\n
124 gl_FragColor = texture2D(sTexture, vTexCoord);\n
125 gl_FragColor *= uColor;
129 Renderer CreateRenderer( const char* vertexSrc, const char* fragmentSrc )
131 Shader shader = Shader::New( vertexSrc, fragmentSrc );
133 Geometry texturedQuadGeometry = Geometry::New();
135 struct VertexPosition { Vector2 position; };
136 struct VertexTexture { Vector2 texture; };
138 VertexPosition positionArray[] =
140 { Vector2( -0.5f, -0.5f ) },
141 { Vector2( 0.5f, -0.5f ) },
142 { Vector2( -0.5f, 0.5f ) },
143 { Vector2( 0.5f, 0.5f ) }
145 uint32_t numberOfVertices = sizeof(positionArray)/sizeof(VertexPosition);
147 VertexTexture uvArray[] =
149 { Vector2( 0.0f, 0.0f ) },
150 { Vector2( 1.0f, 0.0f ) },
151 { Vector2( 0.0f, 1.0f ) },
152 { Vector2( 1.0f, 1.0f ) }
155 Property::Map positionVertexFormat;
156 positionVertexFormat["aPosition"] = Property::VECTOR2;
157 PropertyBuffer positionVertices = PropertyBuffer::New( positionVertexFormat );
158 positionVertices.SetData( positionArray, numberOfVertices );
159 texturedQuadGeometry.AddVertexBuffer( positionVertices );
161 Property::Map textureVertexFormat;
162 textureVertexFormat["aTexture"] = Property::VECTOR2;
163 PropertyBuffer textureVertices = PropertyBuffer::New( textureVertexFormat );
164 textureVertices.SetData( uvArray, numberOfVertices );
165 texturedQuadGeometry.AddVertexBuffer( textureVertices );
167 const uint16_t indices[] = { 0, 3, 1, 0, 2, 3 };
168 texturedQuadGeometry.SetIndexBuffer ( &indices[0], sizeof( indices )/ sizeof( indices[0] ) );
170 Renderer renderer = Renderer::New( texturedQuadGeometry, shader );
172 TextureSet textureSet = TextureSet::New();
173 renderer.SetTextures( textureSet );
178 void SetRendererTexture( Renderer& renderer, Texture& texture )
182 TextureSet textureSet = renderer.GetTextures();
183 textureSet.SetTexture( 0u, texture );
187 void SetRendererTexture( Renderer& renderer, FrameBuffer& frameBuffer )
191 Texture texture = frameBuffer.GetColorTexture();
192 SetRendererTexture( renderer, texture );
210 const unsigned int DEFAULT_BLUR_LEVEL(5u); ///< The default blur level when creating SuperBlurView from the type registry
214 return Toolkit::SuperBlurView::New( DEFAULT_BLUR_LEVEL );
217 // Setup properties, signals and actions using the type-registry.
218 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::SuperBlurView, Toolkit::Control, Create )
220 DALI_PROPERTY_REGISTRATION( Toolkit, SuperBlurView, "imageUrl", STRING, IMAGE_URL )
222 DALI_TYPE_REGISTRATION_END()
224 } // unnamed namespace
226 SuperBlurView::SuperBlurView( unsigned int blurLevels )
227 : Control( ControlBehaviour( DISABLE_SIZE_NEGOTIATION | DISABLE_STYLE_CHANGE_SIGNALS ) ),
228 mTargetSize( Vector2::ZERO ),
229 mBlurStrengthPropertyIndex(Property::INVALID_INDEX),
230 mBlurLevels( blurLevels ),
231 mResourcesCleared( true )
233 DALI_ASSERT_ALWAYS( mBlurLevels > 0 && " Minimal blur level is one, otherwise no blur is needed" );
234 mGaussianBlurView.assign( blurLevels, Toolkit::GaussianBlurView() );
235 mBlurredImage.assign( blurLevels, FrameBuffer() );
236 mRenderers.assign( blurLevels+1, Dali::Renderer() );
239 SuperBlurView::~SuperBlurView()
243 Toolkit::SuperBlurView SuperBlurView::New( unsigned int blurLevels )
245 //Create the implementation
246 IntrusivePtr<SuperBlurView> superBlurView( new SuperBlurView( blurLevels ) );
248 //Pass ownership to CustomActor via derived handle
249 Toolkit::SuperBlurView handle( *superBlurView );
251 // Second-phase init of the implementation
252 // This can only be done after the CustomActor connection has been made...
253 superBlurView->Initialize();
258 void SuperBlurView::OnInitialize()
260 Actor self( Self() );
262 mBlurStrengthPropertyIndex = self.RegisterProperty( "blurStrength", 0.f );
265 void SuperBlurView::SetTexture( Texture texture )
267 mInputTexture = texture;
269 if( mTargetSize == Vector2::ZERO )
276 Actor self( Self() );
278 BlurTexture( 0, mInputTexture );
279 SetRendererTexture( mRenderers[0], texture );
282 for(; i<mBlurLevels; i++)
284 BlurTexture( i, mBlurredImage[i-1].GetColorTexture() );
285 SetRendererTexture( mRenderers[i], mBlurredImage[i-1] );
288 SetRendererTexture( mRenderers[i], mBlurredImage[i-1] );
290 mResourcesCleared = false;
293 Property::Index SuperBlurView::GetBlurStrengthPropertyIndex() const
295 return mBlurStrengthPropertyIndex;
298 void SuperBlurView::SetBlurStrength( float blurStrength )
300 Self().SetProperty(mBlurStrengthPropertyIndex, blurStrength);
303 float SuperBlurView::GetCurrentBlurStrength() const
306 (Self().GetProperty( mBlurStrengthPropertyIndex )).Get(blurStrength);
311 Toolkit::SuperBlurView::SuperBlurViewSignal& SuperBlurView::BlurFinishedSignal()
313 return mBlurFinishedSignal;
316 Texture SuperBlurView::GetBlurredTexture( unsigned int level )
318 DALI_ASSERT_ALWAYS( level>0 && level<=mBlurLevels );
320 FrameBuffer frameBuffer = mBlurredImage[level-1];
322 return frameBuffer.GetColorTexture();
325 void SuperBlurView::BlurTexture( unsigned int idx, Texture texture )
327 DALI_ASSERT_ALWAYS( mGaussianBlurView.size()>idx );
328 mGaussianBlurView[idx] = Toolkit::GaussianBlurView::New( GAUSSIAN_BLUR_DEFAULT_NUM_SAMPLES+GAUSSIAN_BLUR_NUM_SAMPLES_INCREMENTATION*idx,
329 GAUSSIAN_BLUR_BELL_CURVE_WIDTH + GAUSSIAN_BLUR_BELL_CURVE_WIDTH_INCREMENTATION*static_cast<float>(idx),
330 GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT,
331 GAUSSIAN_BLUR_DOWNSAMPLE_WIDTH_SCALE, GAUSSIAN_BLUR_DOWNSAMPLE_HEIGHT_SCALE, true );
332 mGaussianBlurView[idx].SetParentOrigin(ParentOrigin::CENTER);
333 mGaussianBlurView[idx].SetSize(mTargetSize);
334 Stage::GetCurrent().Add( mGaussianBlurView[idx] );
336 mGaussianBlurView[idx].SetUserImageAndOutputRenderTarget( texture, mBlurredImage[idx] );
338 mGaussianBlurView[idx].ActivateOnce();
339 if( idx == mBlurLevels-1 )
341 mGaussianBlurView[idx].FinishedSignal().Connect( this, &SuperBlurView::OnBlurViewFinished );
345 void SuperBlurView::OnBlurViewFinished( Toolkit::GaussianBlurView blurView )
348 Toolkit::SuperBlurView handle( GetOwner() );
349 mBlurFinishedSignal.Emit( handle );
352 void SuperBlurView::ClearBlurResource()
354 if( !mResourcesCleared )
356 DALI_ASSERT_ALWAYS( mGaussianBlurView.size() == mBlurLevels && "must synchronize the GaussianBlurView group if blur levels got changed " );
357 for(unsigned int i=0; i<mBlurLevels;i++)
359 Stage::GetCurrent().Remove( mGaussianBlurView[i] );
360 mGaussianBlurView[i].Deactivate();
362 mResourcesCleared = true;
366 void SuperBlurView::OnSizeSet( const Vector3& targetSize )
368 if( mTargetSize != Vector2(targetSize) )
370 mTargetSize = Vector2(targetSize);
373 for( unsigned int i = 1; i <= mBlurLevels; i++ )
375 float exponent = static_cast<float>(i);
377 unsigned int width = mTargetSize.width/std::pow(2.f,exponent);
378 unsigned int height = mTargetSize.height/std::pow(2.f,exponent);
380 mBlurredImage[i-1] = FrameBuffer::New( width, height, FrameBuffer::Attachment::NONE );
381 Texture texture = Texture::New( TextureType::TEXTURE_2D, GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT, unsigned(width), unsigned(height) );
382 mBlurredImage[i-1].AttachColorTexture( texture );
387 SetTexture( mInputTexture );
391 Control::OnSizeSet( targetSize );
394 void SuperBlurView::OnStageConnection( int depth )
396 if( mTargetSize == Vector2::ZERO )
401 // Exception to the rule, chaining up first ensures visuals have SetOnStage called to create their renderers
402 Control::OnStageConnection( depth );
406 for(unsigned int i=0; i<mBlurLevels+1;i++)
408 mRenderers[i] = CreateRenderer( BASIC_VERTEX_SOURCE, FRAGMENT_SHADER );
409 mRenderers[i].SetProperty( Dali::Renderer::Property::DEPTH_INDEX, (int)i );
410 self.AddRenderer( mRenderers[i] );
414 Renderer renderer = mRenderers[i];
415 Property::Index index = renderer.RegisterProperty( ALPHA_UNIFORM_NAME, 0.f );
416 Constraint constraint = Constraint::New<float>( renderer, index, ActorOpacityConstraint(mBlurLevels, i-1) );
417 constraint.AddSource( Source( self, mBlurStrengthPropertyIndex ) );
424 SetRendererTexture( mRenderers[0], mInputTexture );
426 for(; i<mBlurLevels; i++)
428 SetRendererTexture( mRenderers[i], mBlurredImage[i-1] );
430 SetRendererTexture( mRenderers[i], mBlurredImage[i-1] );
434 void SuperBlurView::OnStageDisconnection()
436 for(unsigned int i=0; i<mBlurLevels+1;i++)
438 Self().RemoveRenderer( mRenderers[i] );
439 mRenderers[i].Reset();
442 Control::OnStageDisconnection();
445 Vector3 SuperBlurView::GetNaturalSize()
449 return Vector3( mInputTexture.GetWidth(), mInputTexture.GetHeight(), 0.f );
451 return Vector3::ZERO;
454 void SuperBlurView::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
456 Toolkit::SuperBlurView superBlurView = Toolkit::SuperBlurView::DownCast( Dali::BaseHandle( object ) );
460 SuperBlurView& superBlurViewImpl( GetImpl( superBlurView ) );
462 if( propertyIndex == Toolkit::SuperBlurView::Property::IMAGE_URL )
464 value.Get( superBlurViewImpl.mUrl );
466 PixelData pixels = SyncImageLoader::Load( superBlurViewImpl.mUrl );
470 Texture texture = Texture::New( TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight() );
471 texture.Upload( pixels, 0, 0, 0, 0, pixels.GetWidth(), pixels.GetHeight() );
473 superBlurViewImpl.SetTexture( texture );
477 DALI_LOG_ERROR( "Cannot create image from property value\n" );
483 Property::Value SuperBlurView::GetProperty( BaseObject* object, Property::Index propertyIndex )
485 Property::Value value;
487 Toolkit::SuperBlurView blurView = Toolkit::SuperBlurView::DownCast( Dali::BaseHandle( object ) );
491 SuperBlurView& superBlurViewImpl( GetImpl( blurView ) );
493 if( propertyIndex == Toolkit::SuperBlurView::Property::IMAGE_URL )
495 value = superBlurViewImpl.mUrl;
502 } // namespace Internal
504 } // namespace Toolkit