[dali_2.3.22] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / svg / svg-visual.cpp
index c6e471c..49d62ab 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 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.
@@ -16,7 +16,7 @@
  */
 
 // CLASS HEADER
-#include "svg-visual.h"
+#include <dali-toolkit/internal/visuals/svg/svg-visual.h>
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
@@ -71,7 +71,7 @@ SvgVisual::SvgVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory&
   mDefaultWidth(0),
   mDefaultHeight(0),
   mPlacementActor(),
-  mRasterizedSize(Vector2::ZERO),
+  mRasterizedSize(-Vector2::ONE), ///< Let we don't use zero since visual size could be zero after trasnform
   mDesiredSize(size),
   mLoadFailed(false),
   mAttemptAtlasing(false)
@@ -92,6 +92,12 @@ SvgVisual::~SvgVisual()
     {
       Dali::AsyncTaskManager::Get().RemoveTask(mRasterizingTask);
     }
+
+    if(mImageUrl.IsBufferResource())
+    {
+      TextureManager& textureManager = mFactoryCache.GetTextureManager();
+      textureManager.RemoveEncodedImageBuffer(mImageUrl.GetUrl());
+    }
   }
 }
 
@@ -105,9 +111,22 @@ void SvgVisual::OnInitialize()
   Vector2 dpi     = Stage::GetCurrent().GetDpi();
   float   meanDpi = (dpi.height + dpi.width) * 0.5f;
 
-  mLoadingTask = new SvgLoadingTask(mVectorRenderer, mImageUrl, meanDpi, MakeCallback(this, &SvgVisual::ApplyRasterizedImage));
+  EncodedImageBuffer encodedImageBuffer;
+
+  if(mImageUrl.IsBufferResource())
+  {
+    // Increase reference count of External Resources :
+    // EncodedImageBuffer.
+    // Reference count will be decreased at destructor of the visual.
+    TextureManager& textureManager = mFactoryCache.GetTextureManager();
+    textureManager.UseExternalResource(mImageUrl.GetUrl());
+
+    encodedImageBuffer = textureManager.GetEncodedImageBuffer(mImageUrl.GetUrl());
+  }
+
+  mLoadingTask = new SvgLoadingTask(mVectorRenderer, mImageUrl, encodedImageBuffer, meanDpi, MakeCallback(this, &SvgVisual::ApplyRasterizedImage));
 
-  if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
+  if(IsSynchronousLoadingRequired() && (mImageUrl.IsLocalResource() || mImageUrl.IsBufferResource()))
   {
     mLoadingTask->Process();
     if(!mLoadingTask->HasSucceeded())
@@ -322,6 +341,10 @@ void SvgVisual::AddRasterizationTask(const Vector2& size)
 
     mRasterizingTask = new SvgRasterizingTask(mVectorRenderer, width, height, MakeCallback(this, &SvgVisual::ApplyRasterizedImage));
 
+#ifdef TRACE_ENABLED
+    reinterpret_cast<SvgRasterizingTask*>(mRasterizingTask.Get())->SetUrl(mImageUrl);
+#endif
+
     if(IsSynchronousLoadingRequired() && mImageUrl.IsLocalResource())
     {
       mRasterizingTask->Process();
@@ -337,6 +360,8 @@ void SvgVisual::AddRasterizationTask(const Vector2& size)
 
 void SvgVisual::ApplyRasterizedImage(SvgTaskPtr task)
 {
+  SvgVisualPtr self = this; // Keep reference until this API finished
+
   if(task->HasSucceeded())
   {
     PixelData rasterizedPixelData = task->GetPixelData();
@@ -345,6 +370,16 @@ void SvgVisual::ApplyRasterizedImage(SvgTaskPtr task)
       task->GetRenderer().GetDefaultSize(mDefaultWidth, mDefaultHeight);
     }
 
+    // We don't need to keep tasks anymore. reset now.
+    if(task == mLoadingTask)
+    {
+      mLoadingTask.Reset();
+    }
+    if(task == mRasterizingTask)
+    {
+      mRasterizingTask.Reset();
+    }
+
     // Rasterization success
     if(rasterizedPixelData && IsOnScene())
     {
@@ -417,6 +452,19 @@ void SvgVisual::ApplyRasterizedImage(SvgTaskPtr task)
   {
     mLoadFailed = true;
 
+    // Remove rasterizing task if we requested before.
+    if(mRasterizingTask)
+    {
+      Dali::AsyncTaskManager::Get().RemoveTask(mRasterizingTask);
+      mRasterizingTask.Reset();
+    }
+
+    // We don't need to keep tasks anymore. reset now.
+    if(task == mLoadingTask)
+    {
+      mLoadingTask.Reset();
+    }
+
     Actor actor = mPlacementActor.GetHandle();
     if(actor)
     {
@@ -428,16 +476,6 @@ void SvgVisual::ApplyRasterizedImage(SvgTaskPtr task)
 
     ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
   }
-
-  // We don't need to keep tasks anymore. reset now.
-  if(task == mLoadingTask)
-  {
-    mLoadingTask.Reset();
-  }
-  if(task == mRasterizingTask)
-  {
-    mRasterizingTask.Reset();
-  }
 }
 
 void SvgVisual::OnSetTransform()