Trigger svg loading early
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / svg / svg-visual.cpp
index bddb7f0..a92f7f8 100644 (file)
@@ -87,6 +87,20 @@ void SvgVisual::OnInitialize()
   Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
   mImpl->mRenderer  = VisualRenderer::New(geometry, shader);
   mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
   Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
   mImpl->mRenderer  = VisualRenderer::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.GetSVGRasterizationThread()->AddTask(newTask);
+  }
 }
 
 void SvgVisual::DoSetProperties(const Property::Map& propertyMap)
 }
 
 void SvgVisual::DoSetProperties(const Property::Map& propertyMap)
@@ -242,16 +256,12 @@ void SvgVisual::AddRasterizationTask(const Vector2& size)
     unsigned int width  = static_cast<unsigned int>(size.width);
     unsigned int height = static_cast<unsigned int>(size.height);
 
     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;
-
-    RasterizingTaskPtr newTask = new RasterizingTask(this, mVectorRenderer, mImageUrl, meanDpi, width, height);
+    SvgTaskPtr newTask = new SvgRasterizingTask(this, mVectorRenderer, width, height);
 
     if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
     {
 
     if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
     {
-      newTask->Load();
-      newTask->Rasterize();
-      ApplyRasterizedImage(newTask->GetPixelData(), newTask->IsLoaded());
+      newTask->Process();
+      ApplyRasterizedImage(newTask->GetPixelData(), newTask->HasSucceeded());
     }
     else
     {
     }
     else
     {
@@ -260,80 +270,84 @@ void SvgVisual::AddRasterizationTask(const Vector2& size)
   }
 }
 
   }
 }
 
-void SvgVisual::ApplyRasterizedImage(PixelData rasterizedPixelData, bool isLoaded)
+void SvgVisual::ApplyRasterizedImage(PixelData rasterizedPixelData, bool success)
 {
 {
-  if(isLoaded && rasterizedPixelData && IsOnScene())
+  if(success)
   {
     if(mDefaultWidth == 0 || mDefaultHeight == 0)
     {
       mVectorRenderer.GetDefaultSize(mDefaultWidth, mDefaultHeight);
     }
 
   {
     if(mDefaultWidth == 0 || mDefaultHeight == 0)
     {
       mVectorRenderer.GetDefaultSize(mDefaultWidth, mDefaultHeight);
     }
 
-    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)
+    // Rasterization success
+    if(rasterizedPixelData && IsOnScene())
     {
     {
-      mFactoryCache.GetAtlasManager()->Remove(currentTextureSet, mAtlasRect);
-    }
-
-    TextureSet textureSet;
+      mRasterizedSize.x = static_cast<float>(rasterizedPixelData.GetWidth());
+      mRasterizedSize.y = static_cast<float>(rasterizedPixelData.GetHeight());
 
 
-    if(mAttemptAtlasing && !mImpl->mCustomShader)
-    {
-      Vector4 atlasRect;
-      textureSet = mFactoryCache.GetAtlasManager()->Add(atlasRect, rasterizedPixelData);
-      if(textureSet) // atlasing
+      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;
 
   {
     mLoadFailed = true;