Support SVG thread pool
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / svg / svg-visual.cpp
index 9a47462..a0ced87 100644 (file)
@@ -27,9 +27,9 @@
 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
 
 // EXTERNAL INCLUDES
-#include <dali/devel-api/adaptor-framework/file-loader.h>
 #include <dali/devel-api/common/stage.h>
 #include <dali/integration-api/debug.h>
+#include <dali/public-api/rendering/decorated-visual-renderer.h>
 
 namespace Dali
 {
@@ -39,7 +39,7 @@ namespace Internal
 {
 namespace
 {
-const int CUSTOM_PROPERTY_COUNT(6); // atlas + corner/border
+const int CUSTOM_PROPERTY_COUNT(1); // atlas
 
 // property name
 const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
@@ -49,7 +49,6 @@ const Dali::Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
 SvgVisualPtr SvgVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties)
 {
   SvgVisualPtr svgVisual(new SvgVisual(factoryCache, shaderFactory, imageUrl));
-  svgVisual->Load();
   svgVisual->SetProperties(properties);
   svgVisual->Initialize();
   return svgVisual;
@@ -58,7 +57,6 @@ SvgVisualPtr SvgVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderF
 SvgVisualPtr SvgVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl)
 {
   SvgVisualPtr svgVisual(new SvgVisual(factoryCache, shaderFactory, imageUrl));
-  svgVisual->Load();
   svgVisual->Initialize();
   return svgVisual;
 }
@@ -72,7 +70,7 @@ SvgVisual::SvgVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory&
   mDefaultWidth(0),
   mDefaultHeight(0),
   mPlacementActor(),
-  mVisualSize(Vector2::ZERO),
+  mRasterizedSize(Vector2::ZERO),
   mLoadFailed(false),
   mAttemptAtlasing(false)
 {
@@ -88,8 +86,22 @@ void SvgVisual::OnInitialize()
 {
   Shader   shader   = GenerateShader();
   Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
-  mImpl->mRenderer  = VisualRenderer::New(geometry, shader);
+  mImpl->mRenderer  = DecoratedVisualRenderer::New(geometry, shader);
   mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
+
+  Vector2 dpi     = Stage::GetCurrent().GetDpi();
+  float   meanDpi = (dpi.height + dpi.width) * 0.5f;
+
+  SvgTaskPtr newTask = new SvgLoadingTask(this, mVectorRenderer, mImageUrl, meanDpi);
+
+  if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
+  {
+    newTask->Process();
+  }
+  else
+  {
+    mFactoryCache.GetSVGRasterizationManager()->AddTask(newTask);
+  }
 }
 
 void SvgVisual::DoSetProperties(const Property::Map& propertyMap)
@@ -169,26 +181,47 @@ void SvgVisual::DoSetOnScene(Actor& actor)
   }
   else
   {
-    // SVG visual needs it's size set before it can be rasterized hence set ResourceReady once on stage
-    ResourceReady(Toolkit::Visual::ResourceStatus::READY);
+    if(mImpl->mEventObserver)
+    {
+      // SVG visual needs it's size set before it can be rasterized hence request relayout once on stage
+      mImpl->mEventObserver->RelayoutRequest(*this);
+    }
   }
 }
 
 void SvgVisual::DoSetOffScene(Actor& actor)
 {
-  mFactoryCache.GetSVGRasterizationThread()->RemoveTask(this);
+  mFactoryCache.GetSVGRasterizationManager()->RemoveTask(this);
 
   actor.RemoveRenderer(mImpl->mRenderer);
   mPlacementActor.Reset();
 
   // Reset the visual size to zero so that when adding the actor back to stage the SVG rasterization is forced
-  mVisualSize = Vector2::ZERO;
+  mRasterizedSize = Vector2::ZERO;
 }
 
 void SvgVisual::GetNaturalSize(Vector2& naturalSize)
 {
-  naturalSize.x = mDefaultWidth;
-  naturalSize.y = mDefaultHeight;
+  if(mLoadFailed && mImpl->mRenderer)
+  {
+    // Load failed, use broken image size
+    auto textureSet = mImpl->mRenderer.GetTextures();
+    if(textureSet && textureSet.GetTextureCount())
+    {
+      auto texture = textureSet.GetTexture(0);
+      if(texture)
+      {
+        naturalSize.x = texture.GetWidth();
+        naturalSize.y = texture.GetHeight();
+        return;
+      }
+    }
+  }
+  else
+  {
+    naturalSize.x = mDefaultWidth;
+    naturalSize.y = mDefaultHeight;
+  }
 }
 
 void SvgVisual::DoCreatePropertyMap(Property::Map& map) const
@@ -217,34 +250,6 @@ void SvgVisual::EnablePreMultipliedAlpha(bool preMultiplied)
   }
 }
 
-void SvgVisual::Load()
-{
-  // load remote resource on svg rasterize thread.
-  if(mImageUrl.IsLocalResource())
-  {
-    Dali::Vector<uint8_t> buffer;
-    if(Dali::FileLoader::ReadFile(mImageUrl.GetUrl(), buffer))
-    {
-      buffer.PushBack('\0');
-
-      Vector2 dpi     = Stage::GetCurrent().GetDpi();
-      float   meanDpi = (dpi.height + dpi.width) * 0.5f;
-      if(!mVectorRenderer.Load(buffer, meanDpi))
-      {
-        mLoadFailed = true;
-        DALI_LOG_ERROR("SvgVisual::Load: Failed to load file! [%s]\n", mImageUrl.GetUrl().c_str());
-        return;
-      }
-      mVectorRenderer.GetDefaultSize(mDefaultWidth, mDefaultHeight);
-    }
-    else
-    {
-      mLoadFailed = true;
-      DALI_LOG_ERROR("SvgVisual::Load: Failed to read file! [%s]\n", mImageUrl.GetUrl().c_str());
-    }
-  }
-}
-
 void SvgVisual::AddRasterizationTask(const Vector2& size)
 {
   if(mImpl->mRenderer)
@@ -252,90 +257,101 @@ void SvgVisual::AddRasterizationTask(const Vector2& size)
     unsigned int width  = static_cast<unsigned int>(size.width);
     unsigned int height = static_cast<unsigned int>(size.height);
 
-    Vector2 dpi     = Stage::GetCurrent().GetDpi();
-    float   meanDpi = (dpi.height + dpi.width) * 0.5f;
+    SvgTaskPtr newTask = new SvgRasterizingTask(this, mVectorRenderer, width, height);
 
-    RasterizingTaskPtr newTask = new RasterizingTask(this, mVectorRenderer, mImageUrl, meanDpi, width, height);
     if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
     {
-      newTask->Load();
-      newTask->Rasterize();
-      ApplyRasterizedImage(newTask->GetVectorRenderer(), newTask->GetPixelData(), newTask->IsLoaded());
+      newTask->Process();
+      ApplyRasterizedImage(newTask->GetPixelData(), newTask->HasSucceeded());
     }
     else
     {
-      mFactoryCache.GetSVGRasterizationThread()->AddTask(newTask);
+      mFactoryCache.GetSVGRasterizationManager()->AddTask(newTask);
     }
   }
 }
 
-void SvgVisual::ApplyRasterizedImage(VectorImageRenderer vectorRenderer, PixelData rasterizedPixelData, bool isLoaded)
+void SvgVisual::ApplyRasterizedImage(PixelData rasterizedPixelData, bool success)
 {
-  if(isLoaded && rasterizedPixelData && IsOnScene())
+  if(success)
   {
-    TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
-    if(mImpl->mFlags & Impl::IS_ATLASING_APPLIED)
+    if(mDefaultWidth == 0 || mDefaultHeight == 0)
     {
-      mFactoryCache.GetAtlasManager()->Remove(currentTextureSet, mAtlasRect);
+      mVectorRenderer.GetDefaultSize(mDefaultWidth, mDefaultHeight);
     }
 
-    TextureSet textureSet;
-
-    if(mAttemptAtlasing && !mImpl->mCustomShader)
+    // Rasterization success
+    if(rasterizedPixelData && IsOnScene())
     {
-      Vector4 atlasRect;
-      textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData);
-      if(textureSet) // atlasing
+      mRasterizedSize.x = static_cast<float>(rasterizedPixelData.GetWidth());
+      mRasterizedSize.y = static_cast<float>(rasterizedPixelData.GetHeight());
+
+      TextureSet currentTextureSet = mImpl->mRenderer.GetTextures();
+      if(mImpl->mFlags & Impl::IS_ATLASING_APPLIED)
       {
-        if(textureSet != currentTextureSet)
-        {
-          mImpl->mRenderer.SetTextures(textureSet);
-        }
-        mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, atlasRect);
-        mAtlasRect = atlasRect;
-        mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
+        mFactoryCache.GetAtlasManager()->Remove(currentTextureSet, mAtlasRect);
       }
-    }
 
-    if(!textureSet) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
-    {
-      Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight());
-      texture.Upload(rasterizedPixelData);
-      mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
+      TextureSet textureSet;
 
-      if(mAtlasRect == FULL_TEXTURE_RECT)
+      if(mAttemptAtlasing && !mImpl->mCustomShader)
       {
-        textureSet = currentTextureSet;
+        Vector4 atlasRect;
+        textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData);
+        if(textureSet) // atlasing
+        {
+          if(textureSet != currentTextureSet)
+          {
+            mImpl->mRenderer.SetTextures(textureSet);
+          }
+          mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, atlasRect);
+          mAtlasRect = atlasRect;
+          mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
+        }
       }
-      else
+
+      if(!textureSet) // no atlasing - mAttemptAtlasing is false or adding to atlas is failed
       {
-        textureSet = TextureSet::New();
-        mImpl->mRenderer.SetTextures(textureSet);
+        Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, rasterizedPixelData.GetWidth(), rasterizedPixelData.GetHeight());
+        texture.Upload(rasterizedPixelData);
+        mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
+
+        if(mAtlasRect == FULL_TEXTURE_RECT)
+        {
+          textureSet = currentTextureSet;
+        }
+        else
+        {
+          textureSet = TextureSet::New();
+          mImpl->mRenderer.SetTextures(textureSet);
+
+          mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT);
+          mAtlasRect = FULL_TEXTURE_RECT;
+        }
 
-        mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, FULL_TEXTURE_RECT);
-        mAtlasRect = FULL_TEXTURE_RECT;
+        if(textureSet)
+        {
+          textureSet.SetTexture(0, texture);
+        }
       }
 
-      if(textureSet)
+      // 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)
       {
-        textureSet.SetTexture(0, texture);
+        actor.AddRenderer(mImpl->mRenderer);
+        // reset the weak handle so that the renderer only get added to actor once
+        mPlacementActor.Reset();
       }
-    }
 
-    // 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();
+      // Svg loaded and ready to display
+      ResourceReady(Toolkit::Visual::ResourceStatus::READY);
     }
-
-    // Svg loaded and ready to display
-    ResourceReady(Toolkit::Visual::ResourceStatus::READY);
   }
-  else if(!isLoaded || !rasterizedPixelData)
+  else if(!success && !mLoadFailed)
   {
+    mLoadFailed = true;
+
     Actor actor = mPlacementActor.GetHandle();
     if(actor)
     {
@@ -355,10 +371,10 @@ void SvgVisual::OnSetTransform()
 
   if(IsOnScene() && !mLoadFailed)
   {
-    if(visualSize != mVisualSize)
+    if(visualSize != mRasterizedSize || mDefaultWidth == 0 || mDefaultHeight == 0)
     {
+      mRasterizedSize = visualSize;
       AddRasterizationTask(visualSize);
-      mVisualSize = visualSize;
     }
   }