Remove OnControl methods & add up-calls
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / gaussian-blur-view / gaussian-blur-view-impl.cpp
index f233cf4..6081c0b 100644 (file)
@@ -24,8 +24,9 @@
 #include <dali/public-api/animation/constraint.h>
 #include <dali/public-api/animation/constraints.h>
 #include <dali/public-api/common/stage.h>
+#include <dali/public-api/object/property-map.h>
 #include <dali/public-api/object/type-registry.h>
-#include <dali/public-api/object/type-registry-helper.h>
+#include <dali/devel-api/object/type-registry-helper.h>
 #include <dali/public-api/render-tasks/render-task-list.h>
 #include <dali/integration-api/debug.h>
 
@@ -36,7 +37,6 @@
 // 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
 
 
@@ -98,17 +98,19 @@ 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 =
+    "varying mediump vec2 vTexCoord;\n"
+    "uniform sampler2D sTexture;\n"
+    "uniform lowp vec4 uColor;\n"
     "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<NUM_SAMPLES; ++i)                                                                      \n"
-    "   {                                                                                                      \n"
-    "     col += texture2D(sTexture, vec2(vTexCoord.x, vTexCoord.y) + uSampleOffsets[i]) * uSampleWeights[i];  \n"
-    "   }                                                                                                      \n"
+    "   mediump vec4 col = texture2D(sTexture, vTexCoord + uSampleOffsets[0]) * uSampleWeights[0];\n"
+    "   for (int i=1; i<NUM_SAMPLES; ++i)\n"
+    "   {\n"
+    "     col += texture2D(sTexture, vTexCoord + uSampleOffsets[i]) * uSampleWeights[i];\n"
+    "   }\n"
     "   gl_FragColor = col;\n"
     "}\n";
 
@@ -130,7 +132,9 @@ GaussianBlurView::GaussianBlurView()
   , mTargetSize(Vector2::ZERO)
   , mLastSize(Vector2::ZERO)
   , mChildrenRoot(Actor::New())
+  , mInternalRoot(Actor::New())
   , mBlurStrengthPropertyIndex(Property::INVALID_INDEX)
+  , mActivated( false )
 {
   SetBlurBellCurveWidth(GAUSSIAN_BLUR_VIEW_DEFAULT_BLUR_BELL_CURVE_WIDTH);
 }
@@ -152,7 +156,9 @@ GaussianBlurView::GaussianBlurView( const unsigned int numSamples, const float b
   , mTargetSize(Vector2::ZERO)
   , mLastSize(Vector2::ZERO)
   , mChildrenRoot(Actor::New())
+  , mInternalRoot(Actor::New())
   , mBlurStrengthPropertyIndex(Property::INVALID_INDEX)
+  , mActivated( false )
 {
   SetBlurBellCurveWidth(blurBellCurveWidth);
 }
@@ -194,7 +200,7 @@ Toolkit::GaussianBlurView GaussianBlurView::New(const unsigned int numSamples, c
 
 /////////////////////////////////////////////////////////////
 // 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
-// TODO: overloading Actor::Add()/Remove() not nice since breaks polymorphism. Need another method to pass ownership of added child actors to our internal actor root.
+// 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);
@@ -245,115 +251,88 @@ 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);
 
   //////////////////////////////////////////////////////
   // Create shaders
 
-  // horiz
   std::ostringstream horizFragmentShaderStringStream;
   horizFragmentShaderStringStream << "#define NUM_SAMPLES " << mNumSamples << "\n";
   horizFragmentShaderStringStream << GAUSSIAN_BLUR_FRAGMENT_SOURCE;
-  mHorizBlurShader = ShaderEffect::New( "", horizFragmentShaderStringStream.str() );
-  // vert
-  std::ostringstream vertFragmentShaderStringStream;
-  vertFragmentShaderStringStream << "#define NUM_SAMPLES " << mNumSamples << "\n";
-  vertFragmentShaderStringStream << GAUSSIAN_BLUR_FRAGMENT_SOURCE;
-  mVertBlurShader = ShaderEffect::New( "", vertFragmentShaderStringStream.str() );
-
+  Property::Map customShader;
+  customShader[ "fragmentShader" ] = horizFragmentShaderStringStream.str();
+  Property::Map rendererMap;
+  rendererMap.Insert( "rendererType", "image" );
+  rendererMap.Insert( "shader", customShader );
 
   //////////////////////////////////////////////////////
   // Create actors
 
   // Create an ImageActor for performing a horizontal blur on the texture
-  mImageActorHorizBlur = ImageActor::New();
+  mImageActorHorizBlur = Toolkit::ImageView::New();
   mImageActorHorizBlur.SetParentOrigin(ParentOrigin::CENTER);
-  mImageActorHorizBlur.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) ); // FIXME
-  mImageActorHorizBlur.SetShaderEffect( mHorizBlurShader );
+  mImageActorHorizBlur.SetProperty( Toolkit::ImageView::Property::IMAGE, rendererMap );
 
   // Create an ImageActor for performing a vertical blur on the texture
-  mImageActorVertBlur = ImageActor::New();
+  mImageActorVertBlur = Toolkit::ImageView::New();
   mImageActorVertBlur.SetParentOrigin(ParentOrigin::CENTER);
-  mImageActorVertBlur.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) ); // FIXME
-  mImageActorVertBlur.SetShaderEffect( mVertBlurShader );
+  mImageActorVertBlur.SetProperty( Toolkit::ImageView::Property::IMAGE, rendererMap );
 
   // Register a property that the user can control to fade the blur in / out via the GaussianBlurView object
-  mBlurStrengthPropertyIndex = Self().RegisterProperty(GAUSSIAN_BLUR_VIEW_STRENGTH_PROPERTY_NAME, GAUSSIAN_BLUR_VIEW_DEFAULT_BLUR_STRENGTH);
+  Actor self = Self();
+  mBlurStrengthPropertyIndex = self.RegisterProperty(GAUSSIAN_BLUR_VIEW_STRENGTH_PROPERTY_NAME, GAUSSIAN_BLUR_VIEW_DEFAULT_BLUR_STRENGTH);
 
   // Create an ImageActor for compositing the blur and the original child actors render
   if(!mBlurUserImage)
   {
-    mImageActorComposite = ImageActor::New();
+    mImageActorComposite = Toolkit::ImageView::New();
     mImageActorComposite.SetParentOrigin(ParentOrigin::CENTER);
-    mImageActorComposite.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) ); // FIXME
     mImageActorComposite.SetOpacity(GAUSSIAN_BLUR_VIEW_DEFAULT_BLUR_STRENGTH); // ensure alpha is enabled for this object and set default value
 
     Constraint blurStrengthConstraint = Constraint::New<float>( mImageActorComposite, Actor::Property::COLOR_ALPHA, EqualToConstraint());
-    blurStrengthConstraint.AddSource( ParentSource(mBlurStrengthPropertyIndex) );
+    blurStrengthConstraint.AddSource( Source( self, mBlurStrengthPropertyIndex) );
     blurStrengthConstraint.Apply();
 
     // 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();
+    mTargetActor = Toolkit::ImageView::New();
     mTargetActor.SetParentOrigin(ParentOrigin::CENTER);
-    mTargetActor.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) ); // FIXME
-
 
     //////////////////////////////////////////////////////
     // 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( mImageActorComposite );
+    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 );
+  Self().Add( mInternalRoot );
+  mInternalRoot.Add( mImageActorHorizBlur );
+  mInternalRoot.Add( mImageActorVertBlur );
+  mInternalRoot.Add( mRenderDownsampledCamera );
 }
 
 
-/**
- * ZrelativeToYconstraint
- *
- * f(current, property, scale) = Vector3(current.x, current.y, property.y * scale)
- */
-struct ZrelativeToYconstraint
+void GaussianBlurView::OnSizeSet(const Vector3& targetSize)
 {
-  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;
+  Control::OnSizeSet( targetSize );
 
-    return v;
-  }
-
-  float mScale;
-};
-
-void GaussianBlurView::OnControlSizeSet(const Vector3& targetSize)
-{
   mTargetSize = Vector2(targetSize);
 
   mChildrenRoot.SetSize(targetSize);
@@ -372,13 +351,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();
+  }
+}
+
+void GaussianBlurView::OnChildAdd( Actor& child )
+{
+  Control::OnChildAdd( child );
+
+  if( child != mChildrenRoot && child != mInternalRoot)
   {
-    AllocateResources();
+    mChildrenRoot.Add( child );
   }
 }
 
+void GaussianBlurView::OnChildRemove( Actor& child )
+{
+  mChildrenRoot.Remove( child );
+
+  Control::OnChildRemove( child );
+}
+
 void GaussianBlurView::AllocateResources()
 {
   // size of render targets etc is based on the size of this actor, ignoring z
@@ -413,13 +410,13 @@ 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 = FrameBufferImage::New( mTargetSize.width, mTargetSize.height, mPixelFormat );
 
       // Set ImageActor for performing a horizontal blur on the texture
       mImageActorHorizBlur.SetImage( mRenderTargetForRenderingChildren );
 
       // Create offscreen buffer for vert blur pass
-      mRenderTarget1 = FrameBufferImage::New( mDownsampledWidth, mDownsampledHeight, mPixelFormat, Dali::Image::UNUSED );
+      mRenderTarget1 = FrameBufferImage::New( mDownsampledWidth, mDownsampledHeight, mPixelFormat );
 
       // use the completed blur in the first buffer and composite with the original child actors render
       mImageActorComposite.SetImage( mRenderTarget1 );
@@ -429,7 +426,7 @@ void GaussianBlurView::AllocateResources()
     }
 
     // Create offscreen buffer for horiz blur pass
-    mRenderTarget2 = FrameBufferImage::New( mDownsampledWidth, mDownsampledHeight, mPixelFormat, Dali::Image::UNUSED );
+    mRenderTarget2 = FrameBufferImage::New( mDownsampledWidth, mDownsampledHeight, mPixelFormat );
 
     // size needs to match render target
     mImageActorHorizBlur.SetSize(mDownsampledWidth, mDownsampledHeight);
@@ -468,6 +465,8 @@ void GaussianBlurView::CreateRenderTasks()
   mHorizBlurTask.SetInputEnabled( false );
   mHorizBlurTask.SetClearEnabled( true );
   mHorizBlurTask.SetClearColor( mBackgroundColor );
+  mHorizBlurTask.SetCameraActor(mRenderDownsampledCamera);
+  mHorizBlurTask.SetTargetFrameBuffer( mRenderTarget2 );
   if( mRenderOnce && mBlurUserImage )
   {
     mHorizBlurTask.SetRefreshRate(RenderTask::REFRESH_ONCE);
@@ -480,6 +479,15 @@ void GaussianBlurView::CreateRenderTasks()
   mVertBlurTask.SetInputEnabled( false );
   mVertBlurTask.SetClearEnabled( true );
   mVertBlurTask.SetClearColor( mBackgroundColor );
+  mVertBlurTask.SetCameraActor(mRenderDownsampledCamera);
+  if(mUserOutputRenderTarget)
+  {
+    mVertBlurTask.SetTargetFrameBuffer( mUserOutputRenderTarget );
+  }
+  else
+  {
+    mVertBlurTask.SetTargetFrameBuffer( mRenderTarget1 );
+  }
   if( mRenderOnce && mBlurUserImage )
   {
     mVertBlurTask.SetRefreshRate(RenderTask::REFRESH_ONCE);
@@ -497,19 +505,6 @@ void GaussianBlurView::CreateRenderTasks()
     mCompositeTask.SetCameraActor(mRenderFullSizeCamera);
     mCompositeTask.SetTargetFrameBuffer( mRenderTargetForRenderingChildren );
   }
-
-  mHorizBlurTask.SetCameraActor(mRenderDownsampledCamera);
-  mVertBlurTask.SetCameraActor(mRenderDownsampledCamera);
-
-  mHorizBlurTask.SetTargetFrameBuffer( mRenderTarget2 );
-  if(mUserOutputRenderTarget)
-  {
-    mVertBlurTask.SetTargetFrameBuffer( mUserOutputRenderTarget );
-  }
-  else
-  {
-    mVertBlurTask.SetTargetFrameBuffer( mRenderTarget1 );
-  }
 }
 
 void GaussianBlurView::RemoveRenderTasks()
@@ -522,25 +517,12 @@ 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
   AllocateResources();
   CreateRenderTasks();
+  mActivated = true;
 }
 
 void GaussianBlurView::ActivateOnce()
@@ -555,7 +537,11 @@ 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;
 }
 
 void GaussianBlurView::SetBlurBellCurveWidth(float blurBellCurveWidth)
@@ -614,11 +600,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 ] );
+    mImageActorHorizBlur.RegisterProperty( GetSampleOffsetsPropertyName( i ), uvOffsets[ i ] * xAxis );
+    mImageActorHorizBlur.RegisterProperty( GetSampleWeightsPropertyName( i ), weights[ i ] );
 
-    mVertBlurShader.SetUniform( GetSampleOffsetsPropertyName( i ), uvOffsets[ i ] * yAxis );
-    mVertBlurShader.SetUniform( GetSampleWeightsPropertyName( i ), weights[ i ] );
+    mImageActorVertBlur.RegisterProperty( GetSampleOffsetsPropertyName( i ), uvOffsets[ i ] * yAxis );
+    mImageActorVertBlur.RegisterProperty( GetSampleWeightsPropertyName( i ), weights[ i ] );
   }
 
   delete[] uvOffsets;