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=7fd6fdf9d04a579eb785c2b812ae861038fd2368;hp=987acbe5f52b038adfa1b967fe0a569a26c34776;hb=8a647e87a01c5c78451653c1264a9eea81ac9b20;hpb=f7c8e7d9a0d3e179e4d9916d2ee11312c4911c1f 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 987acbe..7fd6fdf 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) 2017 Samsung Electronics Co., Ltd. + * Copyright (c) 2020 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. @@ -23,18 +23,20 @@ #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 @@ -100,37 +102,6 @@ const float GAUSSIAN_BLUR_VIEW_DEFAULT_DOWNSAMPLE_HEIGHT_SCALE = 0.5f; const float ARBITRARY_FIELD_OF_VIEW = Math::PI / 4.0f; -#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 -); - const char* const GAUSSIAN_BLUR_FRAGMENT_SOURCE = DALI_COMPOSE_SHADER( varying mediump vec2 vTexCoord;\n uniform sampler2D sTexture;\n @@ -149,72 +120,6 @@ const char* const GAUSSIAN_BLUR_FRAGMENT_SOURCE = DALI_COMPOSE_SHADER( }\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 SetTexture( Actor actor, Texture texture ) -{ - if( Renderer renderer = actor.GetRendererAt(0) ) - { - TextureSet textureSet = renderer.GetTextures(); - textureSet.SetTexture( 0u, texture ); - } -} - -void SetTexture( Actor actor, FrameBuffer frameBuffer ) -{ - if( frameBuffer ) - { - SetTexture( actor, frameBuffer.GetColorTexture() ); - } -} - } // namespace @@ -238,6 +143,10 @@ GaussianBlurView::GaussianBlurView() mActivated( false ) { SetBlurBellCurveWidth(GAUSSIAN_BLUR_VIEW_DEFAULT_BLUR_BELL_CURVE_WIDTH); + DevelControl::SetAccessibilityConstructor( Self(), []( Dali::Actor actor ) { + return std::unique_ptr< Dali::Accessibility::Accessible >( + new Control::Impl::AccessibleImpl( actor, Dali::Accessibility::Role::FILLER ) ); + } ); } GaussianBlurView::GaussianBlurView( const unsigned int numSamples, @@ -302,19 +211,6 @@ Toolkit::GaussianBlurView GaussianBlurView::New(const unsigned int numSamples, c return handle; } -///////////////////////////////////////////////////////////// -// for creating a subtree for all user added child actors, so that we can have them exclusive to the mRenderChildrenTask and our other actors exclusive to our other tasks -// DEPRECATED: overloading Actor::Add()/Remove() not nice since breaks polymorphism. Need another method to pass ownership of added child actors to our internal actor root. -void GaussianBlurView::Add(Actor child) -{ - mChildrenRoot.Add(child); -} - -void GaussianBlurView::Remove(Actor child) -{ - mChildrenRoot.Remove(child); -} - void GaussianBlurView::SetUserImageAndOutputRenderTarget(Texture inputImage, FrameBuffer outputRenderTarget) { // can only do this if the GaussianBlurView object was created with this parameter set @@ -322,7 +218,7 @@ void GaussianBlurView::SetUserImageAndOutputRenderTarget(Texture inputImage, Fra mUserInputImage = inputImage; - SetTexture( mHorizBlurActor, inputImage ); + SetRendererTexture( mHorizBlurActor.GetRendererAt(0), inputImage ); mUserOutputRenderTarget = outputRenderTarget; } @@ -355,8 +251,8 @@ Vector4 GaussianBlurView::GetBackgroundColor() const void GaussianBlurView::OnInitialize() { // root actor to parent all user added actors, needed to allow us to set that subtree as exclusive for our child render task - mChildrenRoot.SetParentOrigin(ParentOrigin::CENTER); - mInternalRoot.SetParentOrigin(ParentOrigin::CENTER); + mChildrenRoot.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER ); + mInternalRoot.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER ); ////////////////////////////////////////////////////// // Create shaders @@ -371,13 +267,13 @@ void GaussianBlurView::OnInitialize() // Create an actor for performing a horizontal blur on the texture mHorizBlurActor = Actor::New(); - mHorizBlurActor.SetParentOrigin(ParentOrigin::CENTER); + mHorizBlurActor.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER ); Renderer renderer = CreateRenderer( BASIC_VERTEX_SOURCE, fragmentSource.c_str() ); mHorizBlurActor.AddRenderer( renderer ); // Create an actor for performing a vertical blur on the texture mVertBlurActor = Actor::New(); - mVertBlurActor.SetParentOrigin(ParentOrigin::CENTER); + mVertBlurActor.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER ); renderer = CreateRenderer( BASIC_VERTEX_SOURCE, fragmentSource.c_str() ); mVertBlurActor.AddRenderer( renderer ); @@ -389,8 +285,8 @@ void GaussianBlurView::OnInitialize() if(!mBlurUserImage) { 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 + mCompositingActor.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER ); + mCompositingActor.SetProperty( Actor::Property::OPACITY,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 ); @@ -400,7 +296,7 @@ void GaussianBlurView::OnInitialize() // 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.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER ); renderer = CreateRenderer( BASIC_VERTEX_SOURCE, BASIC_FRAGMENT_SOURCE ); mTargetActor.AddRenderer( renderer ); @@ -408,7 +304,7 @@ void GaussianBlurView::OnInitialize() // Create cameras for the renders corresponding to the view size mRenderFullSizeCamera = CameraActor::New(); mRenderFullSizeCamera.SetInvertYAxis( true ); - mRenderFullSizeCamera.SetParentOrigin(ParentOrigin::CENTER); + mRenderFullSizeCamera.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER ); ////////////////////////////////////////////////////// // Connect to actor tree @@ -421,12 +317,11 @@ void GaussianBlurView::OnInitialize() // Create camera for the renders corresponding to the (potentially downsampled) render targets' size mRenderDownsampledCamera = CameraActor::New(); mRenderDownsampledCamera.SetInvertYAxis( true ); - mRenderDownsampledCamera.SetParentOrigin(ParentOrigin::CENTER); + mRenderDownsampledCamera.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER ); ////////////////////////////////////////////////////// // Connect to actor tree Self().Add( mChildrenRoot ); - Self().Add( mInternalRoot ); mInternalRoot.Add( mHorizBlurActor ); mInternalRoot.Add( mVertBlurActor ); mInternalRoot.Add( mRenderDownsampledCamera ); @@ -437,19 +332,19 @@ void GaussianBlurView::OnSizeSet(const Vector3& targetSize) { mTargetSize = Vector2(targetSize); - mChildrenRoot.SetSize(targetSize); + mChildrenRoot.SetProperty( Actor::Property::SIZE, targetSize); if( !mBlurUserImage ) { - mCompositingActor.SetSize(targetSize); - mTargetActor.SetSize(targetSize); + mCompositingActor.SetProperty( Actor::Property::SIZE, targetSize); + mTargetActor.SetProperty( Actor::Property::SIZE, 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 // accordingly so they still exactly fill the render target. Note that this means the effective resolution of the child render changes as the GaussianBlurView object changes // size, this is the trade off for not being able to modify render target size // Change camera z position based on GaussianBlurView actor height float cameraPosConstraintScale = 0.5f / tanf(ARBITRARY_FIELD_OF_VIEW * 0.5f); - mRenderFullSizeCamera.SetZ(mTargetSize.height * cameraPosConstraintScale); + mRenderFullSizeCamera.SetProperty( Actor::Property::POSITION_Z, mTargetSize.height * cameraPosConstraintScale); } @@ -482,72 +377,68 @@ void GaussianBlurView::OnChildRemove( Actor& child ) void GaussianBlurView::AllocateResources() { - // size of render targets etc is based on the size of this actor, ignoring z - if(mTargetSize != mLastSize) - { - mLastSize = mTargetSize; + mLastSize = mTargetSize; - // get size of downsampled render targets - mDownsampledWidth = mTargetSize.width * mDownsampleWidthScale; - mDownsampledHeight = mTargetSize.height * mDownsampleHeightScale; + // get size of downsampled render targets + mDownsampledWidth = mTargetSize.width * mDownsampleWidthScale; + mDownsampledHeight = mTargetSize.height * mDownsampleHeightScale; - // Create and place a camera for the renders corresponding to the (potentially downsampled) render targets' size - mRenderDownsampledCamera.SetFieldOfView(ARBITRARY_FIELD_OF_VIEW); + // Create and place a camera for the renders corresponding to the (potentially downsampled) render targets' size + mRenderDownsampledCamera.SetFieldOfView(ARBITRARY_FIELD_OF_VIEW); + // TODO: how do we pick a reasonable value for near clip? Needs to relate to normal camera the user renders with, but we don't have a handle on it + mRenderDownsampledCamera.SetNearClippingPlane(1.0f); + mRenderDownsampledCamera.SetAspectRatio(mDownsampledWidth / mDownsampledHeight); + mRenderDownsampledCamera.SetType(Dali::Camera::FREE_LOOK); // camera orientation based solely on actor + + mRenderDownsampledCamera.SetProperty( Actor::Property::POSITION, Vector3(0.0f, 0.0f, ((mDownsampledHeight * 0.5f) / tanf(ARBITRARY_FIELD_OF_VIEW * 0.5f)))); + + // setup for normal operation + if(!mBlurUserImage) + { + // Create and place a camera for the children render, corresponding to its render target size + mRenderFullSizeCamera.SetFieldOfView(ARBITRARY_FIELD_OF_VIEW); // TODO: how do we pick a reasonable value for near clip? Needs to relate to normal camera the user renders with, but we don't have a handle on it - mRenderDownsampledCamera.SetNearClippingPlane(1.0f); - mRenderDownsampledCamera.SetAspectRatio(mDownsampledWidth / mDownsampledHeight); - mRenderDownsampledCamera.SetType(Dali::Camera::FREE_LOOK); // camera orientation based solely on actor + mRenderFullSizeCamera.SetNearClippingPlane(1.0f); + mRenderFullSizeCamera.SetAspectRatio(mTargetSize.width / mTargetSize.height); + mRenderFullSizeCamera.SetType(Dali::Camera::FREE_LOOK); // camera orientation based solely on actor - mRenderDownsampledCamera.SetPosition(0.0f, 0.0f, ((mDownsampledHeight * 0.5f) / tanf(ARBITRARY_FIELD_OF_VIEW * 0.5f))); + float cameraPosConstraintScale = 0.5f / tanf(ARBITRARY_FIELD_OF_VIEW * 0.5f); + mRenderFullSizeCamera.SetProperty( Actor::Property::POSITION, Vector3(0.0f, 0.0f, mTargetSize.height * cameraPosConstraintScale)); - // setup for normal operation - if(!mBlurUserImage) - { - // Create and place a camera for the children render, corresponding to its render target size - mRenderFullSizeCamera.SetFieldOfView(ARBITRARY_FIELD_OF_VIEW); - // TODO: how do we pick a reasonable value for near clip? Needs to relate to normal camera the user renders with, but we don't have a handle on it - mRenderFullSizeCamera.SetNearClippingPlane(1.0f); - mRenderFullSizeCamera.SetAspectRatio(mTargetSize.width / mTargetSize.height); - mRenderFullSizeCamera.SetType(Dali::Camera::FREE_LOOK); // camera orientation based solely on actor - - float cameraPosConstraintScale = 0.5f / tanf(ARBITRARY_FIELD_OF_VIEW * 0.5f); - mRenderFullSizeCamera.SetPosition(0.0f, 0.0f, mTargetSize.height * cameraPosConstraintScale); - - // create offscreen buffer of new size to render our child actors to - 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 actor for performing a horizontal blur - SetTexture( mHorizBlurActor, mRenderTargetForRenderingChildren ); - - // Create offscreen buffer for vert blur pass - 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 - SetTexture( mCompositingActor, mRenderTarget1 ); - - // set up target actor for rendering result, i.e. the blurred image - SetTexture( mTargetActor, mRenderTargetForRenderingChildren ); - } + // create offscreen buffer of new size to render our child actors to + 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 ); - // Create offscreen buffer for horiz blur pass - mRenderTarget2 = FrameBuffer::New( mDownsampledWidth, mDownsampledHeight, FrameBuffer::Attachment::NONE ); - Texture texture = Texture::New(TextureType::TEXTURE_2D, mPixelFormat, unsigned(mDownsampledWidth), unsigned(mDownsampledHeight)); - mRenderTarget2.AttachColorTexture( texture ); + // Set actor for performing a horizontal blur + SetRendererTexture( mHorizBlurActor.GetRendererAt(0), mRenderTargetForRenderingChildren ); - // size needs to match render target - mHorizBlurActor.SetSize(mDownsampledWidth, mDownsampledHeight); + // Create offscreen buffer for vert blur pass + mRenderTarget1 = FrameBuffer::New( mDownsampledWidth, mDownsampledHeight, FrameBuffer::Attachment::NONE ); + texture = Texture::New(TextureType::TEXTURE_2D, mPixelFormat, unsigned(mDownsampledWidth), unsigned(mDownsampledHeight)); + mRenderTarget1.AttachColorTexture( texture ); - // size needs to match render target - mVertBlurActor.SetSize(mDownsampledWidth, mDownsampledHeight); - SetTexture( mVertBlurActor, mRenderTarget2 ); + // use the completed blur in the first buffer and composite with the original child actors render + SetRendererTexture( mCompositingActor.GetRendererAt(0), mRenderTarget1 ); - // set gaussian blur up for new sized render targets - SetShaderConstants(); + // set up target actor for rendering result, i.e. the blurred image + SetRendererTexture( mTargetActor.GetRendererAt(0), mRenderTargetForRenderingChildren ); } + + // Create offscreen buffer for horiz blur pass + 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 + mHorizBlurActor.SetProperty( Actor::Property::SIZE, Vector2(mDownsampledWidth, mDownsampledHeight) ); + + // size needs to match render target + mVertBlurActor.SetProperty( Actor::Property::SIZE, Vector2(mDownsampledWidth, mDownsampledHeight) ); + SetRendererTexture( mVertBlurActor.GetRendererAt(0), mRenderTarget2 ); + + // set gaussian blur up for new sized render targets + SetShaderConstants(); } void GaussianBlurView::CreateRenderTasks() @@ -566,6 +457,11 @@ void GaussianBlurView::CreateRenderTasks() mRenderChildrenTask.SetCameraActor(mRenderFullSizeCamera); mRenderChildrenTask.SetFrameBuffer( mRenderTargetForRenderingChildren ); + + if( mRenderOnce ) + { + mRenderChildrenTask.SetRefreshRate(RenderTask::REFRESH_ONCE); + } } // perform a horizontal blur targeting the second buffer @@ -577,7 +473,7 @@ void GaussianBlurView::CreateRenderTasks() mHorizBlurTask.SetClearColor( mBackgroundColor ); mHorizBlurTask.SetCameraActor(mRenderDownsampledCamera); mHorizBlurTask.SetFrameBuffer( mRenderTarget2 ); - if( mRenderOnce && mBlurUserImage ) + if( mRenderOnce || ( mRenderOnce && mBlurUserImage ) ) { mHorizBlurTask.SetRefreshRate(RenderTask::REFRESH_ONCE); } @@ -598,7 +494,7 @@ void GaussianBlurView::CreateRenderTasks() { mVertBlurTask.SetFrameBuffer( mRenderTarget1 ); } - if( mRenderOnce && mBlurUserImage ) + if( mRenderOnce || ( mRenderOnce && mBlurUserImage ) ) { mVertBlurTask.SetRefreshRate(RenderTask::REFRESH_ONCE); mVertBlurTask.FinishedSignal().Connect( this, &GaussianBlurView::OnRenderTaskFinished ); @@ -614,6 +510,11 @@ void GaussianBlurView::CreateRenderTasks() mCompositeTask.SetCameraActor(mRenderFullSizeCamera); mCompositeTask.SetFrameBuffer( mRenderTargetForRenderingChildren ); + + if( mRenderOnce ) + { + mCompositeTask.SetRefreshRate(RenderTask::REFRESH_ONCE); + } } } @@ -629,29 +530,37 @@ void GaussianBlurView::RemoveRenderTasks() void GaussianBlurView::Activate() { - // make sure resources are allocated and start the render tasks processing - AllocateResources(); - CreateRenderTasks(); - mActivated = true; + if( !mActivated ) + { + // make sure resources are allocated and start the render tasks processing + Self().Add( mInternalRoot ); + AllocateResources(); + CreateRenderTasks(); + mActivated = true; + } } void GaussianBlurView::ActivateOnce() { - DALI_ASSERT_ALWAYS(mBlurUserImage); // Only works with blurring image mode. + Deactivate(); mRenderOnce = true; Activate(); } void GaussianBlurView::Deactivate() { - // stop render tasks processing - // Note: render target resources are automatically freed since we set the Image::Unused flag - RemoveRenderTasks(); - mRenderTargetForRenderingChildren.Reset(); - mRenderTarget1.Reset(); - mRenderTarget2.Reset(); - mRenderOnce = false; - mActivated = false; + if( mActivated ) + { + // stop render tasks processing + // Note: render target resources are automatically freed since we set the Image::Unused flag + mInternalRoot.Unparent(); + mRenderTargetForRenderingChildren.Reset(); + mRenderTarget1.Reset(); + mRenderTarget2.Reset(); + RemoveRenderTasks(); + mRenderOnce = false; + mActivated = false; + } } void GaussianBlurView::SetBlurBellCurveWidth(float blurBellCurveWidth)