/*
- * 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.
#include <iomanip>
#include <dali/public-api/animation/constraint.h>
#include <dali/public-api/animation/constraints.h>
-#include <dali/public-api/common/stage.h>
+#include <dali/devel-api/common/stage.h>
#include <dali/public-api/object/type-registry.h>
#include <dali/public-api/object/type-registry-helper.h>
#include <dali/public-api/rendering/geometry.h>
-#include <dali/public-api/rendering/property-buffer.h>
#include <dali/public-api/rendering/renderer.h>
#include <dali/public-api/rendering/shader.h>
#include <dali/public-api/render-tasks/render-task-list.h>
#include <dali/integration-api/debug.h>
+#include <dali/devel-api/actors/actor-devel.h>
// INTERNAL INCLUDES
#include <dali-toolkit/public-api/visuals/visual-properties.h>
+#include <dali-toolkit/internal/controls/control/control-renderers.h>
// TODO:
// pixel format / size - set from JSON
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
}\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
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
mUserInputImage = inputImage;
- SetTexture( mHorizBlurActor, inputImage );
+ SetRendererTexture( mHorizBlurActor.GetRendererAt(0), inputImage );
mUserOutputRenderTarget = outputRenderTarget;
}
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
// 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 );
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 );
// 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 );
// 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
// 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 );
{
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);
}
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()
mRenderChildrenTask.SetCameraActor(mRenderFullSizeCamera);
mRenderChildrenTask.SetFrameBuffer( mRenderTargetForRenderingChildren );
+
+ if( mRenderOnce )
+ {
+ mRenderChildrenTask.SetRefreshRate(RenderTask::REFRESH_ONCE);
+ }
}
// perform a horizontal blur targeting the second buffer
mHorizBlurTask.SetClearColor( mBackgroundColor );
mHorizBlurTask.SetCameraActor(mRenderDownsampledCamera);
mHorizBlurTask.SetFrameBuffer( mRenderTarget2 );
- if( mRenderOnce && mBlurUserImage )
+ if( mRenderOnce || ( mRenderOnce && mBlurUserImage ) )
{
mHorizBlurTask.SetRefreshRate(RenderTask::REFRESH_ONCE);
}
{
mVertBlurTask.SetFrameBuffer( mRenderTarget1 );
}
- if( mRenderOnce && mBlurUserImage )
+ if( mRenderOnce || ( mRenderOnce && mBlurUserImage ) )
{
mVertBlurTask.SetRefreshRate(RenderTask::REFRESH_ONCE);
mVertBlurTask.FinishedSignal().Connect( this, &GaussianBlurView::OnRenderTaskFinished );
mCompositeTask.SetCameraActor(mRenderFullSizeCamera);
mCompositeTask.SetFrameBuffer( mRenderTargetForRenderingChildren );
+
+ if( mRenderOnce )
+ {
+ mCompositeTask.SetRefreshRate(RenderTask::REFRESH_ONCE);
+ }
}
}
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)