X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fcontrols%2Fsuper-blur-view%2Fsuper-blur-view-impl.cpp;h=78f354b1f7082f0a31feac6dc32978d38d452df7;hb=124aa28a0cd62b61142779066432cd29195c42ce;hp=c88636fc7f294ffd786ea236c3c2dc76b7a83511;hpb=99e2ea03e6d6059f5803d700932df1ff1c848cd3;p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git diff --git a/dali-toolkit/internal/controls/super-blur-view/super-blur-view-impl.cpp b/dali-toolkit/internal/controls/super-blur-view/super-blur-view-impl.cpp index c88636f..78f354b 100644 --- a/dali-toolkit/internal/controls/super-blur-view/super-blur-view-impl.cpp +++ b/dali-toolkit/internal/controls/super-blur-view/super-blur-view-impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * Copyright (c) 2017 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,7 +30,10 @@ #include // INTERNAL_INCLUDES +#include +#include #include +#include namespace //Unnamed namespace { @@ -92,6 +95,104 @@ struct ActorOpacityConstraint Vector2 mRange; }; +#define DALI_COMPOSE_SHADER(STR) #STR + +const char * const BASIC_VERTEX_SOURCE = DALI_COMPOSE_SHADER( + precision mediump float;\n + attribute mediump vec2 aPosition;\n + attribute mediump vec2 aTexture;\n + varying mediump vec2 vTexCoord;\n + uniform mediump mat4 uMvpMatrix;\n + uniform mediump vec3 uSize;\n + \n + void main()\n + {\n + mediump vec4 vertexPosition = vec4(aPosition * uSize.xy, 0.0, 1.0);\n + vTexCoord = aTexture;\n + gl_Position = uMvpMatrix * vertexPosition;\n + }\n +); + +const char * const BASIC_FRAGMENT_SOURCE = DALI_COMPOSE_SHADER( + precision mediump float;\n + varying mediump vec2 vTexCoord;\n + uniform sampler2D sTexture;\n + uniform vec4 uColor;\n + \n + void main()\n + {\n + gl_FragColor = texture2D(sTexture, vTexCoord);\n + gl_FragColor *= uColor; + }\n +); + +Renderer CreateRenderer( const char* vertexSrc, const char* fragmentSrc ) +{ + Shader shader = Shader::New( vertexSrc, fragmentSrc ); + + Geometry texturedQuadGeometry = Geometry::New(); + + struct VertexPosition { Vector2 position; }; + struct VertexTexture { Vector2 texture; }; + + VertexPosition positionArray[] = + { + { Vector2( -0.5f, -0.5f ) }, + { Vector2( 0.5f, -0.5f ) }, + { Vector2( -0.5f, 0.5f ) }, + { Vector2( 0.5f, 0.5f ) } + }; + uint32_t numberOfVertices = sizeof(positionArray)/sizeof(VertexPosition); + + VertexTexture uvArray[] = + { + { Vector2( 0.0f, 0.0f ) }, + { Vector2( 1.0f, 0.0f ) }, + { Vector2( 0.0f, 1.0f ) }, + { Vector2( 1.0f, 1.0f ) } + }; + + Property::Map positionVertexFormat; + positionVertexFormat["aPosition"] = Property::VECTOR2; + PropertyBuffer positionVertices = PropertyBuffer::New( positionVertexFormat ); + positionVertices.SetData( positionArray, numberOfVertices ); + texturedQuadGeometry.AddVertexBuffer( positionVertices ); + + Property::Map textureVertexFormat; + textureVertexFormat["aTexture"] = Property::VECTOR2; + PropertyBuffer textureVertices = PropertyBuffer::New( textureVertexFormat ); + textureVertices.SetData( uvArray, numberOfVertices ); + texturedQuadGeometry.AddVertexBuffer( textureVertices ); + + const uint16_t indices[] = { 0, 3, 1, 0, 2, 3 }; + texturedQuadGeometry.SetIndexBuffer ( &indices[0], sizeof( indices )/ sizeof( indices[0] ) ); + + Renderer renderer = Renderer::New( texturedQuadGeometry, shader ); + + TextureSet textureSet = TextureSet::New(); + renderer.SetTextures( textureSet ); + + return renderer; +} + +void SetRendererTexture( Renderer& renderer, Texture& texture ) +{ + if( renderer ) + { + TextureSet textureSet = renderer.GetTextures(); + textureSet.SetTexture( 0u, texture ); + } +} + +void SetRendererTexture( Renderer& renderer, FrameBuffer& frameBuffer ) +{ + if( frameBuffer ) + { + Texture texture = frameBuffer.GetColorTexture(); + SetRendererTexture( renderer, texture ); + } +} + } // namespace namespace Dali @@ -116,14 +217,14 @@ BaseHandle Create() // Setup properties, signals and actions using the type-registry. DALI_TYPE_REGISTRATION_BEGIN( Toolkit::SuperBlurView, Toolkit::Control, Create ) -DALI_PROPERTY_REGISTRATION( Toolkit, SuperBlurView, "image", MAP, IMAGE ) +DALI_PROPERTY_REGISTRATION( Toolkit, SuperBlurView, "imageUrl", STRING, IMAGE_URL ) DALI_TYPE_REGISTRATION_END() } // unnamed namespace SuperBlurView::SuperBlurView( unsigned int blurLevels ) -: Control( ControlBehaviour( DISABLE_SIZE_NEGOTIATION ) ), +: Control( ControlBehaviour( DISABLE_SIZE_NEGOTIATION | DISABLE_STYLE_CHANGE_SIGNALS ) ), mTargetSize( Vector2::ZERO ), mBlurStrengthPropertyIndex(Property::INVALID_INDEX), mBlurLevels( blurLevels ), @@ -131,8 +232,8 @@ SuperBlurView::SuperBlurView( unsigned int blurLevels ) { DALI_ASSERT_ALWAYS( mBlurLevels > 0 && " Minimal blur level is one, otherwise no blur is needed" ); mGaussianBlurView.assign( blurLevels, Toolkit::GaussianBlurView() ); - mBlurredImage.assign( blurLevels, FrameBufferImage() ); - mVisuals.assign( blurLevels+1, Toolkit::Visual::Base() ); + mBlurredImage.assign( blurLevels, FrameBuffer() ); + mRenderers.assign( blurLevels+1, Dali::Renderer() ); } SuperBlurView::~SuperBlurView() @@ -156,40 +257,37 @@ Toolkit::SuperBlurView SuperBlurView::New( unsigned int blurLevels ) void SuperBlurView::OnInitialize() { - mBlurStrengthPropertyIndex = Self().RegisterProperty( "blurStrength", 0.f ); + Actor self( Self() ); + + mBlurStrengthPropertyIndex = self.RegisterProperty( "blurStrength", 0.f ); } -void SuperBlurView::SetImage(Image inputImage) +void SuperBlurView::SetTexture( Texture texture ) { - if( mTargetSize == Vector2::ZERO || mInputImage == inputImage) + mInputTexture = texture; + + if( mTargetSize == Vector2::ZERO ) { return; } ClearBlurResource(); - mInputImage = inputImage; Actor self( Self() ); - InitializeVisual( self, mVisuals[0], mInputImage ); - mVisuals[0].SetDepthIndex(0); - SetShaderEffect( mVisuals[0] ); - if( self.OnStage() ) - { - mVisuals[0].SetOnStage( self ); - } - BlurImage( 0, inputImage); - for(unsigned int i=1; i0 && level<=mBlurLevels ); - return mBlurredImage[level-1]; + + FrameBuffer frameBuffer = mBlurredImage[level-1]; + + return frameBuffer.GetColorTexture(); } -void SuperBlurView::BlurImage( unsigned int idx, Image image ) +void SuperBlurView::BlurTexture( unsigned int idx, Texture texture ) { DALI_ASSERT_ALWAYS( mGaussianBlurView.size()>idx ); mGaussianBlurView[idx] = Toolkit::GaussianBlurView::New( GAUSSIAN_BLUR_DEFAULT_NUM_SAMPLES+GAUSSIAN_BLUR_NUM_SAMPLES_INCREMENTATION*idx, @@ -231,7 +332,9 @@ void SuperBlurView::BlurImage( unsigned int idx, Image image ) mGaussianBlurView[idx].SetParentOrigin(ParentOrigin::CENTER); mGaussianBlurView[idx].SetSize(mTargetSize); Stage::GetCurrent().Add( mGaussianBlurView[idx] ); - mGaussianBlurView[idx].SetUserImageAndOutputRenderTarget( image, mBlurredImage[idx] ); + + mGaussianBlurView[idx].SetUserImageAndOutputRenderTarget( texture, mBlurredImage[idx] ); + mGaussianBlurView[idx].ActivateOnce(); if( idx == mBlurLevels-1 ) { @@ -259,15 +362,6 @@ void SuperBlurView::ClearBlurResource() mResourcesCleared = true; } } -void SuperBlurView::SetShaderEffect( Toolkit::Visual::Base& visual ) -{ - Property::Map shaderMap; - std::stringstream verterShaderString; - shaderMap[ "fragmentShader" ] = FRAGMENT_SHADER; - - Internal::Visual::Base& rendererImpl = Toolkit::GetImplementation( visual ); - rendererImpl.SetCustomShader( shaderMap ); -} void SuperBlurView::OnSizeSet( const Vector3& targetSize ) { @@ -279,68 +373,70 @@ void SuperBlurView::OnSizeSet( const Vector3& targetSize ) for( unsigned int i = 1; i <= mBlurLevels; i++ ) { float exponent = static_cast(i); - mBlurredImage[i-1] = FrameBufferImage::New( mTargetSize.width/std::pow(2.f,exponent) , mTargetSize.height/std::pow(2.f,exponent), - GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT, Dali::Image::NEVER ); - InitializeVisual( self, mVisuals[i], mBlurredImage[i - 1] ); - mVisuals[ i ].SetDepthIndex( i ); - SetShaderEffect( mVisuals[ i ] ); - } - if( mInputImage ) - { - SetImage( mInputImage ); + unsigned int width = mTargetSize.width/std::pow(2.f,exponent); + unsigned int height = mTargetSize.height/std::pow(2.f,exponent); + + mBlurredImage[i-1] = FrameBuffer::New( width, height, FrameBuffer::Attachment::NONE ); + Texture texture = Texture::New( TextureType::TEXTURE_2D, GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT, unsigned(width), unsigned(height) ); + mBlurredImage[i-1].AttachColorTexture( texture ); } - if( self.OnStage() ) + if( mInputTexture ) { - for( unsigned int i = 1; i <= mBlurLevels; i++ ) - { - mVisuals[i].SetOnStage( self ); - } + SetTexture( mInputTexture ); } } + + Control::OnSizeSet( targetSize ); } void SuperBlurView::OnStageConnection( int depth ) { - Control::OnStageConnection( depth ); - if( mTargetSize == Vector2::ZERO ) { return; } + // Exception to the rule, chaining up first ensures visuals have SetOnStage called to create their renderers + Control::OnStageConnection( depth ); + Actor self = Self(); - if( mVisuals[0] ) + + for(unsigned int i=0; i 0 ) + { + Renderer renderer = mRenderers[i]; + Property::Index index = renderer.RegisterProperty( ALPHA_UNIFORM_NAME, 0.f ); + Constraint constraint = Constraint::New( renderer, index, ActorOpacityConstraint(mBlurLevels, i-1) ); + constraint.AddSource( Source( self, mBlurStrengthPropertyIndex ) ); + constraint.Apply(); + } } - for(unsigned int i=1; i<=mBlurLevels;i++) + + if( mInputTexture ) { - if( mVisuals[i] ) + SetRendererTexture( mRenderers[0], mInputTexture ); + unsigned int i = 1; + for(; i( renderer, index, ActorOpacityConstraint(mBlurLevels, i-1) ); - constraint.AddSource( Source( self, mBlurStrengthPropertyIndex ) ); - constraint.Apply(); + SetRendererTexture( mRenderers[i], mBlurredImage[i-1] ); } } -void SuperBlurView::OnStageDisconnection( ) +void SuperBlurView::OnStageDisconnection() { - if( mTargetSize == Vector2::ZERO ) - { - return; - } - - Actor self = Self(); for(unsigned int i=0; i