Fix compile error. Non initialized variable.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / super-blur-view / super-blur-view-impl.cpp
index 581b87e..6ed846f 100644 (file)
@@ -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.
@@ -21,7 +21,7 @@
 // EXTERNAL INCLUDES
 #include <cmath>
 #include <dali/public-api/animation/constraint.h>
-#include <dali/public-api/common/stage.h>
+#include <dali/devel-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>
@@ -30,7 +30,9 @@
 #include <dali/integration-api/debug.h>
 
 // INTERNAL_INCLUDES
+#include <dali-toolkit/public-api/image-loader/sync-image-loader.h>
 #include <dali-toolkit/devel-api/controls/control-devel.h>
+#include <dali-toolkit/internal/controls/control/control-renderers.h>
 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
 
@@ -118,7 +120,7 @@ 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()
 
@@ -133,8 +135,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()
@@ -158,12 +160,15 @@ 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 )
 {
-  mInputImage = inputImage;
+  mInputTexture = texture;
+
   if( mTargetSize == Vector2::ZERO )
   {
     return;
@@ -173,23 +178,19 @@ void SuperBlurView::SetImage(Image inputImage)
 
   Actor self( Self() );
 
-  mVisuals[0] = Toolkit::VisualFactory::Get().CreateVisual( mInputImage );
-  DevelControl::RegisterVisual( *this, 0, mVisuals[0] ); // Will clean up previously registered visuals for this index.
-  mVisuals[0].SetDepthIndex(0);
-  // custom shader is not applied on the original image.
+  BlurTexture( 0, mInputTexture );
+  SetRendererTexture( mRenderers[0], texture );
 
-  BlurImage( 0,  inputImage);
-  for(unsigned int i=1; i<mBlurLevels;i++)
+  unsigned int i = 1;
+  for(; i<mBlurLevels; i++)
   {
-    BlurImage( i, mBlurredImage[i-1]);
+    BlurTexture( i, mBlurredImage[i-1].GetColorTexture() );
+    SetRendererTexture( mRenderers[i], mBlurredImage[i-1] );
   }
 
-  mResourcesCleared = false;
-}
+  SetRendererTexture( mRenderers[i], mBlurredImage[i-1] );
 
-Image SuperBlurView::GetImage()
-{
-  return mInputImage;
+  mResourcesCleared = false;
 }
 
 Property::Index SuperBlurView::GetBlurStrengthPropertyIndex() const
@@ -215,23 +216,28 @@ Toolkit::SuperBlurView::SuperBlurViewSignal& SuperBlurView::BlurFinishedSignal()
   return mBlurFinishedSignal;
 }
 
-Image SuperBlurView::GetBlurredImage( unsigned int level )
+Texture SuperBlurView::GetBlurredTexture( unsigned int level )
 {
   DALI_ASSERT_ALWAYS( level>0 && 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,
                                                            GAUSSIAN_BLUR_BELL_CURVE_WIDTH + GAUSSIAN_BLUR_BELL_CURVE_WIDTH_INCREMENTATION*static_cast<float>(idx),
                                                            GAUSSIAN_BLUR_RENDER_TARGET_PIXEL_FORMAT,
                                                            GAUSSIAN_BLUR_DOWNSAMPLE_WIDTH_SCALE, GAUSSIAN_BLUR_DOWNSAMPLE_HEIGHT_SCALE, true );
-  mGaussianBlurView[idx].SetParentOrigin(ParentOrigin::CENTER);
-  mGaussianBlurView[idx].SetSize(mTargetSize);
+  mGaussianBlurView[idx].SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER );
+  mGaussianBlurView[idx].SetProperty( Actor::Property::SIZE, 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 +265,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& visualImpl = Toolkit::GetImplementation( visual );
-  visualImpl.SetCustomShader( shaderMap );
-}
 
 void SuperBlurView::OnSizeSet( const Vector3& targetSize )
 {
@@ -279,58 +276,80 @@ void SuperBlurView::OnSizeSet( const Vector3& targetSize )
     for( unsigned int i = 1; i <= mBlurLevels; i++ )
     {
       float exponent = static_cast<float>(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 );
 
-      mVisuals[i] = Toolkit::VisualFactory::Get().CreateVisual( mBlurredImage[i - 1] );
-      DevelControl::RegisterVisual( *this, i, mVisuals[i] ); // Will clean up existing visual with same index.
-      mVisuals[i].SetDepthIndex( i );
-      SetShaderEffect( mVisuals[i] );
+      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( mInputImage )
+    if( mInputTexture )
     {
-      SetImage( mInputImage );
+      SetTexture( mInputTexture );
     }
   }
 
   Control::OnSizeSet( targetSize );
 }
 
-void SuperBlurView::OnStageConnection( int depth )
+void SuperBlurView::OnSceneConnection( int 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 );
+  // Exception to the rule, chaining up first ensures visuals have SetOnScene called to create their renderers
+  Control::OnSceneConnection( depth );
 
   Actor self = Self();
-  for(unsigned int i=0; i<=mBlurLevels;i++)
+
+  for(unsigned int i=0; i<mBlurLevels+1;i++)
   {
-    // Note that the renderer indices are depending on the order they been added to the actor
-    // which might be different from the blur level of its texture.
-    // We can check the depth index of the renderer to know which blurred image it renders.
-    // All visuals WILL have renderers at this point as we are simply creating visuals with an Image handle.
-    Renderer renderer = self.GetRendererAt( i );
-    int depthIndex = renderer.GetProperty<int>(Renderer::Property::DEPTH_INDEX);
-    if( depthIndex > 0 )
+    mRenderers[i] = CreateRenderer( BASIC_VERTEX_SOURCE, FRAGMENT_SHADER );
+    mRenderers[i].SetProperty( Dali::Renderer::Property::DEPTH_INDEX, (int)i );
+    self.AddRenderer( mRenderers[i] );
+
+    if( i > 0 )
     {
+      Renderer renderer = mRenderers[i];
       Property::Index index = renderer.RegisterProperty( ALPHA_UNIFORM_NAME, 0.f );
-      Constraint constraint = Constraint::New<float>( renderer, index, ActorOpacityConstraint(mBlurLevels, depthIndex-1) );
+      Constraint constraint = Constraint::New<float>( renderer, index, ActorOpacityConstraint(mBlurLevels, i-1) );
       constraint.AddSource( Source( self, mBlurStrengthPropertyIndex ) );
       constraint.Apply();
     }
   }
+
+  if( mInputTexture )
+  {
+    SetRendererTexture( mRenderers[0], mInputTexture );
+    unsigned int i = 1;
+    for(; i<mBlurLevels; i++)
+    {
+      SetRendererTexture( mRenderers[i], mBlurredImage[i-1] );
+    }
+    SetRendererTexture( mRenderers[i], mBlurredImage[i-1] );
+  }
+}
+
+void SuperBlurView::OnSceneDisconnection()
+{
+  for(unsigned int i=0; i<mBlurLevels+1;i++)
+  {
+    Self().RemoveRenderer( mRenderers[i] );
+    mRenderers[i].Reset();
+  }
+
+  Control::OnSceneDisconnection();
 }
 
 Vector3 SuperBlurView::GetNaturalSize()
 {
-  if( mInputImage )
+  if( mInputTexture )
   {
-    return Vector3( mInputImage.GetWidth(), mInputImage.GetHeight(), 0.f );
+    return Vector3( mInputTexture.GetWidth(), mInputTexture.GetHeight(), 0.f );
   }
   return Vector3::ZERO;
 }
@@ -343,12 +362,18 @@ void SuperBlurView::SetProperty( BaseObject* object, Property::Index propertyInd
   {
     SuperBlurView& superBlurViewImpl( GetImpl( superBlurView ) );
 
-    if( propertyIndex == Toolkit::SuperBlurView::Property::IMAGE )
+    if( propertyIndex == Toolkit::SuperBlurView::Property::IMAGE_URL )
     {
-      Dali::Image image = Scripting::NewImage( value );
-      if ( image )
+      value.Get( superBlurViewImpl.mUrl );
+
+      PixelData pixels = SyncImageLoader::Load( superBlurViewImpl.mUrl );
+
+      if ( pixels )
       {
-        superBlurViewImpl.SetImage( image );
+        Texture texture = Texture::New( TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight() );
+        texture.Upload( pixels, 0, 0, 0, 0, pixels.GetWidth(), pixels.GetHeight() );
+
+        superBlurViewImpl.SetTexture( texture );
       }
       else
       {
@@ -368,15 +393,9 @@ Property::Value SuperBlurView::GetProperty( BaseObject* object, Property::Index
   {
     SuperBlurView& superBlurViewImpl( GetImpl( blurView ) );
 
-    if( propertyIndex == Toolkit::SuperBlurView::Property::IMAGE )
+    if( propertyIndex == Toolkit::SuperBlurView::Property::IMAGE_URL )
     {
-      Property::Map map;
-      Image inputImage = superBlurViewImpl.GetImage();
-      if( inputImage )
-      {
-        Scripting::CreatePropertyMap( inputImage, map );
-      }
-      value = Property::Value( map );
+      value = superBlurViewImpl.mUrl;
     }
   }