[dali_2.3.24] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / image / image-visual.cpp
index fd0f2b7..02d08af 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.
@@ -104,31 +104,31 @@ constexpr uint32_t TEXTURE_COUNT_FOR_GPU_ALPHA_MASK = 2u;
 struct NameIndexMatch
 {
   const char* const name;
-  Property::Index  index;
+  Property::Index   index;
 };
 
 const NameIndexMatch NAME_INDEX_MATCH_TABLE[] =
-{
-  {IMAGE_FITTING_MODE, Toolkit::ImageVisual::Property::FITTING_MODE},
-  {IMAGE_SAMPLING_MODE, Toolkit::ImageVisual::Property::SAMPLING_MODE},
-  {IMAGE_DESIRED_WIDTH, Toolkit::ImageVisual::Property::DESIRED_WIDTH},
-  {IMAGE_DESIRED_HEIGHT, Toolkit::ImageVisual::Property::DESIRED_HEIGHT},
-  {PIXEL_AREA_UNIFORM_NAME, Toolkit::ImageVisual::Property::PIXEL_AREA},
-  {IMAGE_WRAP_MODE_U, Toolkit::ImageVisual::Property::WRAP_MODE_U},
-  {IMAGE_WRAP_MODE_V, Toolkit::ImageVisual::Property::WRAP_MODE_V},
-  {SYNCHRONOUS_LOADING, Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING},
-  {IMAGE_ATLASING, Toolkit::ImageVisual::Property::ATLASING},
-  {ALPHA_MASK_URL, Toolkit::ImageVisual::Property::ALPHA_MASK_URL},
-  {MASK_CONTENT_SCALE_NAME, Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE},
-  {CROP_TO_MASK_NAME, Toolkit::ImageVisual::Property::CROP_TO_MASK},
-  {MASKING_TYPE_NAME, Toolkit::DevelImageVisual::Property::MASKING_TYPE},
-  {ENABLE_BROKEN_IMAGE, Toolkit::DevelImageVisual::Property::ENABLE_BROKEN_IMAGE},
-  {LOAD_POLICY_NAME, Toolkit::ImageVisual::Property::LOAD_POLICY},
-  {RELEASE_POLICY_NAME, Toolkit::ImageVisual::Property::RELEASE_POLICY},
-  {ORIENTATION_CORRECTION_NAME, Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION},
-  {FAST_TRACK_UPLOADING_NAME, Toolkit::DevelImageVisual::Property::FAST_TRACK_UPLOADING},
+  {
+    {IMAGE_FITTING_MODE, Toolkit::ImageVisual::Property::FITTING_MODE},
+    {IMAGE_SAMPLING_MODE, Toolkit::ImageVisual::Property::SAMPLING_MODE},
+    {IMAGE_DESIRED_WIDTH, Toolkit::ImageVisual::Property::DESIRED_WIDTH},
+    {IMAGE_DESIRED_HEIGHT, Toolkit::ImageVisual::Property::DESIRED_HEIGHT},
+    {PIXEL_AREA_UNIFORM_NAME, Toolkit::ImageVisual::Property::PIXEL_AREA},
+    {IMAGE_WRAP_MODE_U, Toolkit::ImageVisual::Property::WRAP_MODE_U},
+    {IMAGE_WRAP_MODE_V, Toolkit::ImageVisual::Property::WRAP_MODE_V},
+    {SYNCHRONOUS_LOADING, Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING},
+    {IMAGE_ATLASING, Toolkit::ImageVisual::Property::ATLASING},
+    {ALPHA_MASK_URL, Toolkit::ImageVisual::Property::ALPHA_MASK_URL},
+    {MASK_CONTENT_SCALE_NAME, Toolkit::ImageVisual::Property::MASK_CONTENT_SCALE},
+    {CROP_TO_MASK_NAME, Toolkit::ImageVisual::Property::CROP_TO_MASK},
+    {MASKING_TYPE_NAME, Toolkit::DevelImageVisual::Property::MASKING_TYPE},
+    {ENABLE_BROKEN_IMAGE, Toolkit::DevelImageVisual::Property::ENABLE_BROKEN_IMAGE},
+    {LOAD_POLICY_NAME, Toolkit::ImageVisual::Property::LOAD_POLICY},
+    {RELEASE_POLICY_NAME, Toolkit::ImageVisual::Property::RELEASE_POLICY},
+    {ORIENTATION_CORRECTION_NAME, Toolkit::ImageVisual::Property::ORIENTATION_CORRECTION},
+    {FAST_TRACK_UPLOADING_NAME, Toolkit::DevelImageVisual::Property::FAST_TRACK_UPLOADING},
 };
-const int NAME_INDEX_MATCH_TABLE_SIZE = sizeof(NAME_INDEX_MATCH_TABLE)/sizeof(NAME_INDEX_MATCH_TABLE[0]);
+const int NAME_INDEX_MATCH_TABLE_SIZE = sizeof(NAME_INDEX_MATCH_TABLE) / sizeof(NAME_INDEX_MATCH_TABLE[0]);
 
 Geometry CreateGeometry(VisualFactoryCache& factoryCache, ImageDimensions gridSize)
 {
@@ -137,7 +137,8 @@ Geometry CreateGeometry(VisualFactoryCache& factoryCache, ImageDimensions gridSi
   if(gridSize == ImageDimensions(1, 1))
   {
     geometry = factoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
-  } else
+  }
+  else
   {
     geometry = VisualFactoryCache::CreateGridGeometry(gridSize);
   }
@@ -533,6 +534,7 @@ void ImageVisual::GetNaturalSize(Vector2& naturalSize)
           imageSize = mPlacementActorSize;
         }
 
+        mUseBrokenImageRenderer = true;
         mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
         Texture brokenImage = mImpl->mRenderer.GetTextures().GetTexture(0);
         naturalSize.x       = brokenImage.GetWidth();
@@ -546,27 +548,6 @@ void ImageVisual::GetNaturalSize(Vector2& naturalSize)
 
 void ImageVisual::OnInitialize()
 {
-  Geometry geometry;
-
-  // Get the geometry
-  if(mImpl->mCustomShader)
-  {
-    geometry = CreateGeometry(mFactoryCache, mImpl->mCustomShader->mGridSize);
-  }
-  else // Get any geometry associated with the texture
-  {
-    TextureManager& textureManager = mFactoryCache.GetTextureManager();
-
-    uint32_t firstElementCount{0u};
-    uint32_t secondElementCount{0u};
-    geometry = textureManager.GetRenderGeometry(mTextureId, firstElementCount, secondElementCount);
-
-    if(!firstElementCount && !secondElementCount) // Otherwise use quad
-    {
-      geometry = CreateGeometry(mFactoryCache, ImageDimensions(1, 1));
-    }
-  }
-
   // Increase reference count of External Resources :
   // EncodedImageBuffer or ExternalTextures.
   // Reference count will be decreased at destructor of the visual.
@@ -576,7 +557,9 @@ void ImageVisual::OnInitialize()
     textureManager.UseExternalResource(mImageUrl.GetUrl());
   }
 
-  Shader shader = GenerateShader();
+  // Generate geometry and shader. Note that we should check AddOn when generate geometry, due to LoadPolicy::IMMEDIATE case
+  Geometry geometry = GenerateGeometry(mTextureId, true);
+  Shader   shader   = GenerateShader();
 
   // Create the renderer
   mImpl->mRenderer = DecoratedVisualRenderer::New(geometry, shader);
@@ -649,7 +632,7 @@ void ImageVisual::LoadTexture(bool& atlasing, Vector4& atlasRect, TextureSet& te
     else if(mUseFastTrackUploading)
     {
       DALI_LOG_DEBUG_INFO("FastTrack : Fail to load fast track. mUrl : [%s]%s%s%s%s%s%s%s%s\n",
-                          mImageUrl.GetUrl().c_str(),
+                          mImageUrl.GetEllipsedUrl().c_str(),
                           (mLoadPolicy != Toolkit::ImageVisual::LoadPolicy::ATTACHED) ? "/ mLoadPolicy != ATTACHED" : "",
                           (mReleasePolicy != Toolkit::ImageVisual::ReleasePolicy::DETACHED) ? "/ mReleasePolicy != DETACHED" : "",
                           (forceReload != TextureManager::ReloadPolicy::CACHED) ? "/ forceReload != CACHED" : "",
@@ -679,9 +662,9 @@ void ImageVisual::LoadTexture(bool& atlasing, Vector4& atlasRect, TextureSet& te
     else
     {
       DALI_ASSERT_ALWAYS(mFastTrackLoadingTask->mTextures.size() >= 3u);
-      textureSet.SetTexture(0u, mFastTrackLoadingTask->mTextures[0]);
-      textureSet.SetTexture(1u, mFastTrackLoadingTask->mTextures[1]);
       textureSet.SetTexture(2u, mFastTrackLoadingTask->mTextures[2]);
+      textureSet.SetTexture(1u, mFastTrackLoadingTask->mTextures[1]);
+      textureSet.SetTexture(0u, mFastTrackLoadingTask->mTextures[0]);
 
       // We cannot determine what kind of shader will be used.
       // Just use unified shader, and then change shader after load completed.
@@ -711,7 +694,7 @@ void ImageVisual::LoadTexture(bool& atlasing, Vector4& atlasRect, TextureSet& te
     }
 
     EnablePreMultipliedAlpha(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
-    if(!atlasing)
+    if(!atlasing && (mWrapModeU != Dali::WrapMode::DEFAULT || mWrapModeV != Dali::WrapMode::DEFAULT))
     {
       Sampler sampler = Sampler::New();
       sampler.SetWrapMode(mWrapModeU, mWrapModeV);
@@ -761,7 +744,9 @@ void ImageVisual::InitializeRenderer()
     else
     {
       mTextures = mFactoryCache.GetTextureManager().GetTextureSet(mTextureId);
-      if(!(mImpl->mFlags & Visual::Base::Impl::IS_ATLASING_APPLIED) && mTextures)
+      if(!(mImpl->mFlags & Visual::Base::Impl::IS_ATLASING_APPLIED) &&
+         mTextures &&
+         (mWrapModeU != Dali::WrapMode::DEFAULT || mWrapModeV != Dali::WrapMode::DEFAULT))
       {
         Sampler sampler = Sampler::New();
         sampler.SetWrapMode(mWrapModeU, mWrapModeV);
@@ -776,7 +761,7 @@ void ImageVisual::InitializeRenderer()
     ComputeTextureSize();
     CheckMaskTexture();
 
-    bool needToUpdateShader = DevelTexture::IsNative(mTextures.GetTexture(0));
+    bool needToUpdateShader = DevelTexture::IsNative(mTextures.GetTexture(0)) || mUseBrokenImageRenderer;
 
     if(mTextures.GetTextureCount() == 3)
     {
@@ -792,6 +777,21 @@ void ImageVisual::InitializeRenderer()
       UpdateShader();
     }
     mTextures.Reset(); // Visual should not keep a handle to the texture after this point.
+
+    if(DALI_UNLIKELY(mUseBrokenImageRenderer))
+    {
+      // We need to re-generate geometry only if it was broken image before, and result changed after Reload.
+      auto geometry = GenerateGeometry(mTextureId, true);
+
+      // Update geometry only if we need.
+      if(geometry)
+      {
+        mImpl->mRenderer.SetGeometry(geometry);
+      }
+    }
+
+    // We don't use broken image anymore.
+    mUseBrokenImageRenderer = false;
   }
 
   if(attemptAtlasing) // the texture is packed inside atlas
@@ -936,6 +936,11 @@ void ImageVisual::OnDoAction(const Dali::Property::Index actionId, const Dali::P
     case DevelImageVisual::Action::RELOAD:
     {
       auto attemptAtlasing = AttemptAtlasing();
+
+      // Reset resource ready status when we call reload.
+      ResourceReady(Toolkit::Visual::ResourceStatus::PREPARING);
+      mLoadState = TextureManager::LoadState::NOT_STARTED;
+
       LoadTexture(attemptAtlasing, mAtlasRect, mTextures, mOrientationCorrection, TextureManager::ReloadPolicy::FORCED);
       break;
     }
@@ -1041,13 +1046,9 @@ void ImageVisual::FastLoadComplete(FastTrackLoadingTaskPtr task)
 void ImageVisual::LoadComplete(bool loadingSuccess, TextureInformation textureInformation)
 {
   Toolkit::Visual::ResourceStatus resourceStatus;
+
   if(mImpl->mRenderer)
   {
-    if(textureInformation.useAtlasing)
-    {
-      mImpl->mRenderer.RegisterProperty(ATLAS_RECT_UNIFORM_NAME, mAtlasRect);
-    }
-
     EnablePreMultipliedAlpha(textureInformation.preMultiplied);
 
     Actor actor = mPlacementActor.GetHandle();
@@ -1058,7 +1059,7 @@ void ImageVisual::LoadComplete(bool loadingSuccess, TextureInformation textureIn
     }
     else
     {
-      if(!textureInformation.useAtlasing)
+      if(mWrapModeU != Dali::WrapMode::DEFAULT || mWrapModeV != Dali::WrapMode::DEFAULT)
       {
         Sampler sampler = Sampler::New();
         sampler.SetWrapMode(mWrapModeU, mWrapModeV);
@@ -1069,15 +1070,22 @@ void ImageVisual::LoadComplete(bool loadingSuccess, TextureInformation textureIn
       ComputeTextureSize();
       CheckMaskTexture();
 
+      bool needToUpdateShader = mUseBrokenImageRenderer;
+
       if(textureInformation.textureSet.GetTextureCount() == 3)
       {
         if(textureInformation.textureSet.GetTexture(0).GetPixelFormat() == Pixel::L8 && textureInformation.textureSet.GetTexture(1).GetPixelFormat() == Pixel::CHROMINANCE_U && textureInformation.textureSet.GetTexture(2).GetPixelFormat() == Pixel::CHROMINANCE_V)
         {
-          mNeedYuvToRgb = true;
-          UpdateShader();
+          mNeedYuvToRgb      = true;
+          needToUpdateShader = true;
         }
       }
 
+      if(needToUpdateShader)
+      {
+        UpdateShader();
+      }
+
       if(actor)
       {
         actor.AddRenderer(mImpl->mRenderer);
@@ -1085,6 +1093,19 @@ void ImageVisual::LoadComplete(bool loadingSuccess, TextureInformation textureIn
         // reset the weak handle so that the renderer only get added to actor once
         mPlacementActor.Reset();
       }
+
+      auto geometry = GenerateGeometry(textureInformation.textureId, mUseBrokenImageRenderer);
+
+      if(DALI_UNLIKELY(geometry))
+      {
+        // Rare cases. If load successed image don't use quad geometry (i.e. Show some n-patch broken image, and call Reload(), and success)
+        // or If given texture use AddOn,
+        // then we need to make to use quad geometry and update shader agian.
+        mImpl->mRenderer.SetGeometry(geometry);
+      }
+
+      // We don't use broken image anymore.
+      mUseBrokenImageRenderer = false;
     }
   }
 
@@ -1106,36 +1127,6 @@ void ImageVisual::LoadComplete(bool loadingSuccess, TextureInformation textureIn
     mLoadState     = TextureManager::LoadState::LOAD_FAILED;
   }
 
-  // use geometry if needed
-  if(loadingSuccess)
-  {
-    uint32_t firstElementCount{0u};
-    uint32_t secondElementCount{0u};
-    auto     geometry = mFactoryCache.GetTextureManager().GetRenderGeometry(mTextureId, firstElementCount, secondElementCount);
-    if(mImpl->mRenderer && geometry)
-    {
-      mImpl->mRenderer.SetGeometry(geometry);
-      Dali::DevelRenderer::DrawCommand drawCommand{};
-      drawCommand.drawType = DevelRenderer::DrawType::INDEXED;
-
-      if(firstElementCount)
-      {
-        drawCommand.firstIndex   = 0;
-        drawCommand.elementCount = firstElementCount;
-        drawCommand.queue        = DevelRenderer::RENDER_QUEUE_OPAQUE;
-        DevelRenderer::AddDrawCommand(mImpl->mRenderer, drawCommand);
-      }
-
-      if(secondElementCount)
-      {
-        drawCommand.firstIndex   = firstElementCount;
-        drawCommand.elementCount = secondElementCount;
-        drawCommand.queue        = DevelRenderer::RENDER_QUEUE_TRANSPARENT;
-        DevelRenderer::AddDrawCommand(mImpl->mRenderer, drawCommand);
-      }
-    }
-  }
-
   // Signal to observers ( control ) that resources are ready. Must be all resources.
   ResourceReady(resourceStatus);
 }
@@ -1330,6 +1321,10 @@ void ImageVisual::CheckMaskTexture()
       {
         mImpl->mRenderer.RegisterProperty(MASK_TEXTURE_RATIO_NAME, ComputeMaskTextureRatio());
       }
+      else
+      {
+        mImpl->mRenderer.RegisterProperty(MASK_TEXTURE_RATIO_NAME, Vector2::ONE);
+      }
       maskLoadFailed = false;
     }
 
@@ -1372,6 +1367,7 @@ void ImageVisual::ShowBrokenImage()
       }
     }
 
+    mUseBrokenImageRenderer = true;
     mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize);
     if(actor)
     {
@@ -1404,6 +1400,59 @@ void ImageVisual::ResetFastTrackLoadingTask()
   }
 }
 
+Geometry ImageVisual::GenerateGeometry(TextureManager::TextureId textureId, bool createForce)
+{
+  Geometry geometry;
+  if(Stage::IsInstalled())
+  {
+    if(mImpl->mCustomShader)
+    {
+      if(createForce)
+      {
+        geometry = CreateGeometry(mFactoryCache, mImpl->mCustomShader->mGridSize);
+      }
+    }
+    else
+    {
+      uint32_t firstElementCount{0u};
+      uint32_t secondElementCount{0u};
+
+      geometry = mFactoryCache.GetTextureManager().GetRenderGeometry(textureId, firstElementCount, secondElementCount);
+      if(geometry)
+      {
+        if(mImpl->mRenderer)
+        {
+          Dali::DevelRenderer::DrawCommand drawCommand{};
+          drawCommand.drawType = DevelRenderer::DrawType::INDEXED;
+
+          if(firstElementCount)
+          {
+            drawCommand.firstIndex   = 0;
+            drawCommand.elementCount = firstElementCount;
+            drawCommand.queue        = DevelRenderer::RENDER_QUEUE_OPAQUE;
+            DevelRenderer::AddDrawCommand(mImpl->mRenderer, drawCommand);
+          }
+
+          if(secondElementCount)
+          {
+            drawCommand.firstIndex   = firstElementCount;
+            drawCommand.elementCount = secondElementCount;
+            drawCommand.queue        = DevelRenderer::RENDER_QUEUE_TRANSPARENT;
+            DevelRenderer::AddDrawCommand(mImpl->mRenderer, drawCommand);
+          }
+        }
+      }
+      else if(createForce)
+      {
+        // Create default quad geometry now
+        geometry = CreateGeometry(mFactoryCache, ImageDimensions(1, 1));
+      }
+    }
+  }
+
+  return geometry;
+}
+
 } // namespace Internal
 
 } // namespace Toolkit