Merge "Add SetProperty and GetProperty to Visuals." into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / svg / svg-visual.cpp
index d13e1e1..6c0bcfb 100644 (file)
 #include <dali/integration-api/debug.h>
 
 // INTERNAL INCLUDES
+#include <dali-toolkit/public-api/visuals/image-visual-properties.h>
 #include <dali-toolkit/third-party/nanosvg/nanosvg.h>
 #include <dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h>
 #include <dali-toolkit/internal/visuals/image/image-visual.h>
+#include <dali-toolkit/internal/visuals/image-atlas-manager.h>
 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
@@ -51,10 +53,9 @@ namespace Toolkit
 namespace Internal
 {
 
-SvgVisual::SvgVisual( VisualFactoryCache& factoryCache, ImageAtlasManager& atlasManager )
+SvgVisual::SvgVisual( VisualFactoryCache& factoryCache )
 : Visual::Base( factoryCache ),
   mAtlasRect( FULL_TEXTURE_RECT ),
-  mAtlasManager( atlasManager ),
   mParsedImage( NULL )
 {
   // the rasterized image is with pre-multiplied alpha format
@@ -69,14 +70,9 @@ SvgVisual::~SvgVisual()
   }
 }
 
-bool SvgVisual::IsSvgUrl( const std::string& url )
-{
-  return url.substr( url.find_last_of(".") + 1 ) == "svg";
-}
-
 void SvgVisual::DoInitialize( Actor& actor, const Property::Map& propertyMap )
 {
-  Property::Value* imageURLValue = propertyMap.Find( IMAGE_URL_NAME );
+  Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
   if( imageURLValue )
   {
     std::string imageUrl;
@@ -93,7 +89,7 @@ void SvgVisual::DoInitialize( Actor& actor, const Property::Map& propertyMap )
 
 void SvgVisual::DoSetOnStage( Actor& actor )
 {
-  Shader shader = ImageVisual::GetImageShader( mFactoryCache );
+  Shader shader = ImageVisual::GetImageShader( mFactoryCache, true, true );
   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
   if( !geometry )
   {
@@ -108,6 +104,9 @@ void SvgVisual::DoSetOnStage( Actor& actor )
   {
     AddRasterizationTask( mImpl->mSize );
   }
+
+  // Hold the weak handle of the placement actor and delay the adding of renderer until the svg rasterization is finished.
+  mPlacementActor = actor;
 }
 
 void SvgVisual::DoSetOffStage( Actor& actor )
@@ -116,6 +115,7 @@ void SvgVisual::DoSetOffStage( Actor& actor )
 
   actor.RemoveRenderer( mImpl->mRenderer );
   mImpl->mRenderer.Reset();
+  mPlacementActor.Reset();
 }
 
 void SvgVisual::GetNaturalSize( Vector2& naturalSize ) const
@@ -143,13 +143,24 @@ void SvgVisual::SetSize( const Vector2& size )
 void SvgVisual::DoCreatePropertyMap( Property::Map& map ) const
 {
   map.Clear();
-  map.Insert( RENDERER_TYPE, IMAGE_RENDERER );
+  map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
   if( !mImageUrl.empty() )
   {
-    map.Insert( IMAGE_URL_NAME, mImageUrl );
+    map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl );
   }
 }
 
+void SvgVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
+{
+  // TODO
+}
+
+Dali::Property::Value SvgVisual::DoGetProperty( Dali::Property::Index index )
+{
+  // TODO
+  return Dali::Property::Value();
+}
+
 void SvgVisual::SetImage( const std::string& imageUrl, ImageDimensions size )
 {
   if( mImageUrl != imageUrl )
@@ -197,11 +208,11 @@ void SvgVisual::ApplyRasterizedImage( PixelData rasterizedPixelData )
     TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
     if( mAtlasRect != FULL_TEXTURE_RECT )
     {
-      mAtlasManager.Remove( currentTextureSet, mAtlasRect );
+      mFactoryCache.GetAtlasManager()->Remove( currentTextureSet, mAtlasRect );
     }
 
     Vector4 atlasRect;
-    TextureSet textureSet = mAtlasManager.Add(atlasRect, rasterizedPixelData );
+    TextureSet textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData );
     if( textureSet ) // atlasing
     {
       if( textureSet != currentTextureSet )
@@ -210,11 +221,13 @@ void SvgVisual::ApplyRasterizedImage( PixelData rasterizedPixelData )
       }
       mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
       mAtlasRect = atlasRect;
+      mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
     }
     else // no atlasing
     {
       Atlas texture = Atlas::New( rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight() );
       texture.Upload( rasterizedPixelData, 0, 0 );
+      mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
 
       if( mAtlasRect == FULL_TEXTURE_RECT )
       {
@@ -234,11 +247,19 @@ void SvgVisual::ApplyRasterizedImage( PixelData rasterizedPixelData )
         TextureSetImage( textureSet, 0u, texture );
       }
     }
+
+    // Rasterized pixels are uploaded to texture. If weak handle is holding a placement actor, it is the time to add the renderer to actor.
+    Actor actor = mPlacementActor.GetHandle();
+    if( actor )
+    {
+      actor.AddRenderer( mImpl->mRenderer );
+      // reset the weak handle so that the renderer only get added to actor once
+      mPlacementActor.Reset();
+    }
   }
 }
 
 
-
 } // namespace Internal
 
 } // namespace Toolkit