X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fcontrols%2Fgaussian-blur-view%2Fgaussian-blur-view-impl.cpp;h=233a79c3877a5c4e93d42d91f9dfa1122565db13;hp=5a5e501ed1b79c269a1e6422354c9c6351e55e78;hb=7d7006623dc5520280ceebd56c102aa76f121c3d;hpb=2ec164cd618f93ccafe17b1d0b8ff16401ed4aef diff --git a/dali-toolkit/internal/controls/gaussian-blur-view/gaussian-blur-view-impl.cpp b/dali-toolkit/internal/controls/gaussian-blur-view/gaussian-blur-view-impl.cpp index 5a5e501..233a79c 100644 --- a/dali-toolkit/internal/controls/gaussian-blur-view/gaussian-blur-view-impl.cpp +++ b/dali-toolkit/internal/controls/gaussian-blur-view/gaussian-blur-view-impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 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. @@ -21,23 +21,26 @@ // EXTERNAL INCLUDES #include #include -#include #include #include #include #include #include +#include +#include +#include +#include #include #include // INTERNAL INCLUDES -#include +#include +#include // TODO: // pixel format / size - set from JSON // aspect ratio property needs to be able to be constrained also for cameras, not possible currently. Therefore changing aspect ratio of GaussianBlurView won't currently work // default near clip value -// mChildrenRoot Add()/Remove() overloads - better solution // Manager object - re-use render targets if there are multiple GaussianBlurViews created @@ -56,13 +59,13 @@ // 2 modes: // 1st mode, this control has a tree of actors (use Add() to add children) that are rendered and blurred. // mRenderChildrenTask renders children to FB mRenderTargetForRenderingChildren -// mHorizBlurTask renders mImageActorHorizBlur Actor showing FB mRenderTargetForRenderingChildren into FB mRenderTarget2 -// mVertBlurTask renders mImageActorVertBlur Actor showing FB mRenderTarget2 into FB mRenderTarget1 -// mCompositeTask renders mImageActorComposite Actor showing FB mRenderTarget1 into FB mRenderTargetForRenderingChildren +// mHorizBlurTask renders mHorizBlurActor Actor showing FB mRenderTargetForRenderingChildren into FB mRenderTarget2 +// mVertBlurTask renders mVertBlurActor Actor showing FB mRenderTarget2 into FB mRenderTarget1 +// mCompositeTask renders mCompositingActor Actor showing FB mRenderTarget1 into FB mRenderTargetForRenderingChildren // // 2nd mode, an image is blurred and rendered to a supplied target framebuffer -// mHorizBlurTask renders mImageActorHorizBlur Actor showing mUserInputImage into FB mRenderTarget2 -// mVertBlurTask renders mImageActorVertBlur Actor showing mRenderTarget2 into FB mUserOutputRenderTarget +// mHorizBlurTask renders mHorizBlurActor Actor showing mUserInputImage into FB mRenderTarget2 +// mVertBlurTask renders mVertBlurActor Actor showing mRenderTarget2 into FB mUserOutputRenderTarget // // Only this 2nd mode handles ActivateOnce @@ -98,62 +101,72 @@ const float GAUSSIAN_BLUR_VIEW_DEFAULT_DOWNSAMPLE_HEIGHT_SCALE = 0.5f; const float ARBITRARY_FIELD_OF_VIEW = Math::PI / 4.0f; -const char* const GAUSSIAN_BLUR_FRAGMENT_SOURCE = - "uniform mediump vec2 uSampleOffsets[NUM_SAMPLES];\n" - "uniform mediump float uSampleWeights[NUM_SAMPLES];\n" - - "void main()\n" - "{\n" - " mediump vec4 col;\n" - " col = texture2D(sTexture, vec2(vTexCoord.x, vTexCoord.y) + uSampleOffsets[0]) * uSampleWeights[0]; \n" - " for (int i=1; i( Actor::Property::COLOR_ALPHA, ParentSource(mBlurStrengthPropertyIndex), EqualToConstraintFloat()); - mImageActorComposite.ApplyConstraint(blurStrengthConstraint); - - // Create an ImageActor for holding final result, i.e. the blurred image. This will get rendered to screen later, via default / user render task - mTargetActor = ImageActor::New(); + mCompositingActor = Actor::New(); + mCompositingActor.SetParentOrigin(ParentOrigin::CENTER); + mCompositingActor.SetOpacity(GAUSSIAN_BLUR_VIEW_DEFAULT_BLUR_STRENGTH); // ensure alpha is enabled for this object and set default value + renderer = CreateRenderer( BASIC_VERTEX_SOURCE, BASIC_FRAGMENT_SOURCE ); + mCompositingActor.AddRenderer( renderer ); + + Constraint blurStrengthConstraint = Constraint::New( mCompositingActor, Actor::Property::COLOR_ALPHA, EqualToConstraint()); + blurStrengthConstraint.AddSource( Source( self, mBlurStrengthPropertyIndex) ); + blurStrengthConstraint.Apply(); + + // Create an image view for holding final result, i.e. the blurred image. This will get rendered to screen later, via default / user render task + mTargetActor = Actor::New(); mTargetActor.SetParentOrigin(ParentOrigin::CENTER); - mTargetActor.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) ); // FIXME - + renderer = CreateRenderer( BASIC_VERTEX_SOURCE, BASIC_FRAGMENT_SOURCE ); + mTargetActor.AddRenderer( renderer ); ////////////////////////////////////////////////////// // Create cameras for the renders corresponding to the view size mRenderFullSizeCamera = CameraActor::New(); + mRenderFullSizeCamera.SetInvertYAxis( true ); mRenderFullSizeCamera.SetParentOrigin(ParentOrigin::CENTER); - ////////////////////////////////////////////////////// // Connect to actor tree - Self().Add( mImageActorComposite ); - Self().Add( mTargetActor ); - Self().Add( mRenderFullSizeCamera ); + mInternalRoot.Add( mCompositingActor ); + mInternalRoot.Add( mTargetActor ); + mInternalRoot.Add( mRenderFullSizeCamera ); } - ////////////////////////////////////////////////////// // Create camera for the renders corresponding to the (potentially downsampled) render targets' size mRenderDownsampledCamera = CameraActor::New(); + mRenderDownsampledCamera.SetInvertYAxis( true ); mRenderDownsampledCamera.SetParentOrigin(ParentOrigin::CENTER); - ////////////////////////////////////////////////////// // Connect to actor tree Self().Add( mChildrenRoot ); - Self().Add( mImageActorHorizBlur ); - Self().Add( mImageActorVertBlur ); - Self().Add( mRenderDownsampledCamera ); + mInternalRoot.Add( mHorizBlurActor ); + mInternalRoot.Add( mVertBlurActor ); + mInternalRoot.Add( mRenderDownsampledCamera ); } -/** - * ZrelativeToYconstraint - * - * f(current, property, scale) = Vector3(current.x, current.y, property.y * scale) - */ -struct ZrelativeToYconstraint -{ - ZrelativeToYconstraint( float scale ) - : mScale( scale ) - {} - - Vector3 operator()(const Vector3& current, - const PropertyInput& property) - { - Vector3 v; - - v.x = current.x; - v.y = current.y; - v.z = property.GetVector3().y * mScale; - - return v; - } - - float mScale; -}; - -void GaussianBlurView::OnControlSizeSet(const Vector3& targetSize) +void GaussianBlurView::OnSizeSet(const Vector3& targetSize) { mTargetSize = Vector2(targetSize); @@ -372,7 +344,7 @@ void GaussianBlurView::OnControlSizeSet(const Vector3& targetSize) if( !mBlurUserImage ) { - mImageActorComposite.SetSize(targetSize); + mCompositingActor.SetSize(targetSize); mTargetActor.SetSize(targetSize); // Children render camera must move when GaussianBlurView object is resized. This is since we cannot change render target size - so we need to remap the child actors' rendering @@ -384,11 +356,31 @@ void GaussianBlurView::OnControlSizeSet(const Vector3& targetSize) } - // if we are already on stage, need to update render target sizes now to reflect the new size of this actor - if(Self().OnStage()) + // if we have already activated the blur, need to update render target sizes now to reflect the new size of this actor + if(mActivated) + { + Deactivate(); + Activate(); + } + + Control::OnSizeSet( targetSize ); +} + +void GaussianBlurView::OnChildAdd( Actor& child ) +{ + if( child != mChildrenRoot && child != mInternalRoot) { - AllocateResources(); + mChildrenRoot.Add( child ); } + + Control::OnChildAdd( child ); +} + +void GaussianBlurView::OnChildRemove( Actor& child ) +{ + mChildrenRoot.Remove( child ); + + Control::OnChildRemove( child ); } void GaussianBlurView::AllocateResources() @@ -425,30 +417,36 @@ void GaussianBlurView::AllocateResources() mRenderFullSizeCamera.SetPosition(0.0f, 0.0f, mTargetSize.height * cameraPosConstraintScale); // create offscreen buffer of new size to render our child actors to - mRenderTargetForRenderingChildren = FrameBufferImage::New( mTargetSize.width, mTargetSize.height, mPixelFormat, Dali::Image::UNUSED ); + mRenderTargetForRenderingChildren = FrameBuffer::New( mTargetSize.width, mTargetSize.height, FrameBuffer::Attachment::NONE ); + Texture texture = Texture::New( TextureType::TEXTURE_2D, mPixelFormat, unsigned(mTargetSize.width), unsigned(mTargetSize.height) ); + mRenderTargetForRenderingChildren.AttachColorTexture( texture ); - // Set ImageActor for performing a horizontal blur on the texture - mImageActorHorizBlur.SetImage( mRenderTargetForRenderingChildren ); + // Set actor for performing a horizontal blur + SetRendererTexture( mHorizBlurActor.GetRendererAt(0), mRenderTargetForRenderingChildren ); // Create offscreen buffer for vert blur pass - mRenderTarget1 = FrameBufferImage::New( mDownsampledWidth, mDownsampledHeight, mPixelFormat, Dali::Image::UNUSED ); + mRenderTarget1 = FrameBuffer::New( mDownsampledWidth, mDownsampledHeight, FrameBuffer::Attachment::NONE ); + texture = Texture::New(TextureType::TEXTURE_2D, mPixelFormat, unsigned(mDownsampledWidth), unsigned(mDownsampledHeight)); + mRenderTarget1.AttachColorTexture( texture ); // use the completed blur in the first buffer and composite with the original child actors render - mImageActorComposite.SetImage( mRenderTarget1 ); + SetRendererTexture( mCompositingActor.GetRendererAt(0), mRenderTarget1 ); // set up target actor for rendering result, i.e. the blurred image - mTargetActor.SetImage(mRenderTargetForRenderingChildren); + SetRendererTexture( mTargetActor.GetRendererAt(0), mRenderTargetForRenderingChildren ); } // Create offscreen buffer for horiz blur pass - mRenderTarget2 = FrameBufferImage::New( mDownsampledWidth, mDownsampledHeight, mPixelFormat, Dali::Image::UNUSED ); + mRenderTarget2 = FrameBuffer::New( mDownsampledWidth, mDownsampledHeight, FrameBuffer::Attachment::NONE ); + Texture texture = Texture::New(TextureType::TEXTURE_2D, mPixelFormat, unsigned(mDownsampledWidth), unsigned(mDownsampledHeight)); + mRenderTarget2.AttachColorTexture( texture ); // size needs to match render target - mImageActorHorizBlur.SetSize(mDownsampledWidth, mDownsampledHeight); + mHorizBlurActor.SetSize(mDownsampledWidth, mDownsampledHeight); // size needs to match render target - mImageActorVertBlur.SetImage( mRenderTarget2 ); - mImageActorVertBlur.SetSize(mDownsampledWidth, mDownsampledHeight); + mVertBlurActor.SetSize(mDownsampledWidth, mDownsampledHeight); + SetRendererTexture( mVertBlurActor.GetRendererAt(0), mRenderTarget2 ); // set gaussian blur up for new sized render targets SetShaderConstants(); @@ -470,16 +468,18 @@ void GaussianBlurView::CreateRenderTasks() mRenderChildrenTask.SetClearColor( mBackgroundColor ); mRenderChildrenTask.SetCameraActor(mRenderFullSizeCamera); - mRenderChildrenTask.SetTargetFrameBuffer( mRenderTargetForRenderingChildren ); + mRenderChildrenTask.SetFrameBuffer( mRenderTargetForRenderingChildren ); } // perform a horizontal blur targeting the second buffer mHorizBlurTask = taskList.CreateTask(); - mHorizBlurTask.SetSourceActor( mImageActorHorizBlur ); + mHorizBlurTask.SetSourceActor( mHorizBlurActor ); mHorizBlurTask.SetExclusive(true); mHorizBlurTask.SetInputEnabled( false ); mHorizBlurTask.SetClearEnabled( true ); mHorizBlurTask.SetClearColor( mBackgroundColor ); + mHorizBlurTask.SetCameraActor(mRenderDownsampledCamera); + mHorizBlurTask.SetFrameBuffer( mRenderTarget2 ); if( mRenderOnce && mBlurUserImage ) { mHorizBlurTask.SetRefreshRate(RenderTask::REFRESH_ONCE); @@ -487,11 +487,20 @@ void GaussianBlurView::CreateRenderTasks() // use the second buffer and perform a horizontal blur targeting the first buffer mVertBlurTask = taskList.CreateTask(); - mVertBlurTask.SetSourceActor( mImageActorVertBlur ); + mVertBlurTask.SetSourceActor( mVertBlurActor ); mVertBlurTask.SetExclusive(true); mVertBlurTask.SetInputEnabled( false ); mVertBlurTask.SetClearEnabled( true ); mVertBlurTask.SetClearColor( mBackgroundColor ); + mVertBlurTask.SetCameraActor(mRenderDownsampledCamera); + if(mUserOutputRenderTarget) + { + mVertBlurTask.SetFrameBuffer( mUserOutputRenderTarget ); + } + else + { + mVertBlurTask.SetFrameBuffer( mRenderTarget1 ); + } if( mRenderOnce && mBlurUserImage ) { mVertBlurTask.SetRefreshRate(RenderTask::REFRESH_ONCE); @@ -502,25 +511,12 @@ void GaussianBlurView::CreateRenderTasks() if(!mBlurUserImage) { mCompositeTask = taskList.CreateTask(); - mCompositeTask.SetSourceActor( mImageActorComposite ); + mCompositeTask.SetSourceActor( mCompositingActor ); mCompositeTask.SetExclusive(true); mCompositeTask.SetInputEnabled( false ); mCompositeTask.SetCameraActor(mRenderFullSizeCamera); - mCompositeTask.SetTargetFrameBuffer( mRenderTargetForRenderingChildren ); - } - - mHorizBlurTask.SetCameraActor(mRenderDownsampledCamera); - mVertBlurTask.SetCameraActor(mRenderDownsampledCamera); - - mHorizBlurTask.SetTargetFrameBuffer( mRenderTarget2 ); - if(mUserOutputRenderTarget) - { - mVertBlurTask.SetTargetFrameBuffer( mUserOutputRenderTarget ); - } - else - { - mVertBlurTask.SetTargetFrameBuffer( mRenderTarget1 ); + mCompositeTask.SetFrameBuffer( mRenderTargetForRenderingChildren ); } } @@ -534,25 +530,13 @@ void GaussianBlurView::RemoveRenderTasks() taskList.RemoveTask(mCompositeTask); } -void GaussianBlurView::OnStageDisconnection() -{ - // TODO: can't call this here, since SetImage() calls fails similarly to above - // Need to fix the stage connection so this callback can be used arbitrarily. At that point we can simplify the API by removing the need for Activate() / Deactivate() - //Deactivate(); -} - -void GaussianBlurView::OnControlStageConnection() -{ - // TODO: can't call this here, since SetImage() calls fail to connect images to stage, since parent chain not fully on stage yet - // Need to fix the stage connection so this callback can be used arbitrarily. At that point we can simplify the API by removing the need for Activate() / Deactivate() - //Activate(); -} - void GaussianBlurView::Activate() { // make sure resources are allocated and start the render tasks processing + Self().Add( mInternalRoot ); AllocateResources(); CreateRenderTasks(); + mActivated = true; } void GaussianBlurView::ActivateOnce() @@ -566,8 +550,13 @@ void GaussianBlurView::Deactivate() { // stop render tasks processing // Note: render target resources are automatically freed since we set the Image::Unused flag + mInternalRoot.Unparent(); RemoveRenderTasks(); + mRenderTargetForRenderingChildren.Reset(); + mRenderTarget1.Reset(); + mRenderTarget2.Reset(); mRenderOnce = false; + mActivated = false; } void GaussianBlurView::SetBlurBellCurveWidth(float blurBellCurveWidth) @@ -626,11 +615,11 @@ void GaussianBlurView::SetShaderConstants() Vector2 yAxis(0.0f, 1.0f); for (i = 0; i < mNumSamples; ++i ) { - mHorizBlurShader.SetUniform( GetSampleOffsetsPropertyName( i ), uvOffsets[ i ] * xAxis ); - mHorizBlurShader.SetUniform( GetSampleWeightsPropertyName( i ), weights[ i ] ); + mHorizBlurActor.RegisterProperty( GetSampleOffsetsPropertyName( i ), uvOffsets[ i ] * xAxis ); + mHorizBlurActor.RegisterProperty( GetSampleWeightsPropertyName( i ), weights[ i ] ); - mVertBlurShader.SetUniform( GetSampleOffsetsPropertyName( i ), uvOffsets[ i ] * yAxis ); - mVertBlurShader.SetUniform( GetSampleWeightsPropertyName( i ), weights[ i ] ); + mVertBlurActor.RegisterProperty( GetSampleOffsetsPropertyName( i ), uvOffsets[ i ] * yAxis ); + mVertBlurActor.RegisterProperty( GetSampleWeightsPropertyName( i ), weights[ i ] ); } delete[] uvOffsets;