[dali_2.3.22] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / npatch / npatch-visual.cpp
index 606e040..1047eaf 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.
@@ -80,7 +80,7 @@ void NPatchVisual::LoadImages()
     bool preMultiplyOnLoad = IsPreMultipliedAlphaEnabled() && !mImpl->mCustomShader ? true : false;
     mId                    = mLoader.Load(textureManager, this, mImageUrl, mBorder, preMultiplyOnLoad, synchronousLoading);
 
-    std::shared_ptr<const NPatchData> data;
+    NPatchDataPtr data;
     if(mLoader.GetNPatchData(mId, data) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE)
     {
       EnablePreMultipliedAlpha(data->IsPreMultiplied());
@@ -105,13 +105,6 @@ void NPatchVisual::LoadImages()
     // Load the auxiliary image
     mAuxiliaryTextureSet = textureManager.LoadTexture(mAuxiliaryUrl, Dali::ImageDimensions(), FittingMode::DEFAULT, SamplingMode::BOX_THEN_LINEAR, maskingDataPtr, synchronousLoading, mAuxiliaryTextureId, atlasRect, atlasRectSize, atlasing, loadingStatus, this, nullptr, imageAtlasManagerPtr, true, TextureManager::ReloadPolicy::CACHED, preMultiplyOnLoad);
 
-    if(mAuxiliaryTextureSet)
-    {
-      Sampler sampler = Sampler::New();
-      sampler.SetWrapMode(WrapMode::DEFAULT, WrapMode::DEFAULT);
-      mAuxiliaryTextureSet.SetSampler(0u, sampler);
-    }
-
     // If synchronousLoading is true, we can check the auxiliaryResource's status now.
     if(synchronousLoading)
     {
@@ -126,7 +119,7 @@ void NPatchVisual::GetNaturalSize(Vector2& naturalSize)
   naturalSize.y = 0u;
 
   // load now if not already loaded
-  std::shared_ptr<const NPatchData> data;
+  NPatchDataPtr data;
   if(mLoader.GetNPatchData(mId, data) && data->GetLoadingState() != NPatchData::LoadingState::LOADING)
   {
     naturalSize.x = data->GetCroppedWidth();
@@ -223,7 +216,7 @@ void NPatchVisual::DoSetOnScene(Actor& actor)
   // at this case, we try to SetResouce to mPlaceActor twice. so, we should avoid that case.
   mPlacementActor = actor;
 
-  std::shared_ptr<const NPatchData> data;
+  NPatchDataPtr data;
   if(mImpl->mRenderer && mLoader.GetNPatchData(mId, data) && data->GetLoadingState() != NPatchData::LoadingState::LOADING)
   {
     // If mAuxiliaryUrl need to be loaded, we should wait it until LoadComplete called.
@@ -338,9 +331,9 @@ NPatchVisual::~NPatchVisual()
 void NPatchVisual::OnInitialize()
 {
   // Get basic geometry and shader
-  Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
-  auto imageVisualShaderFeatureBuilder = ImageVisualShaderFeatureBuilder();
-  Shader   shader   = mImageVisualShaderFactory.GetShader(
+  Geometry geometry                        = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
+  auto     imageVisualShaderFeatureBuilder = ImageVisualShaderFeatureBuilder();
+  Shader   shader                          = mImageVisualShaderFactory.GetShader(
     mFactoryCache,
     imageVisualShaderFeatureBuilder);
 
@@ -353,8 +346,8 @@ void NPatchVisual::OnInitialize()
 
 Geometry NPatchVisual::CreateGeometry()
 {
-  Geometry                          geometry;
-  std::shared_ptr<const NPatchData> data;
+  Geometry      geometry;
+  NPatchDataPtr data;
   if(mLoader.GetNPatchData(mId, data) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE)
   {
     if(data->GetStretchPixelsX().Size() == 1 && data->GetStretchPixelsY().Size() == 1)
@@ -408,8 +401,8 @@ Geometry NPatchVisual::CreateGeometry()
 
 Shader NPatchVisual::CreateShader()
 {
-  Shader                            shader;
-  std::shared_ptr<const NPatchData> data;
+  Shader        shader;
+  NPatchDataPtr data;
   // 0 is either no data (load failed?) or no stretch regions on image
   // for both cases we use the default shader
   NPatchUtility::StretchRanges::SizeType xStretchCount = 0;
@@ -435,19 +428,20 @@ Shader NPatchVisual::CreateShader()
       shader = mFactoryCache.GetShader(shaderType);
       if(DALI_UNLIKELY(!shader))
       {
-        shader = Shader::New(SHADER_NPATCH_VISUAL_3X3_SHADER_VERT, fragmentShader);
-        // Only cache vanilla 9 patch shaders
-        mFactoryCache.SaveShader(shaderType, shader);
+        shader = mFactoryCache.GenerateAndSaveShader(shaderType, SHADER_NPATCH_VISUAL_3X3_SHADER_VERT, fragmentShader);
       }
     }
     else if(xStretchCount > 0 || yStretchCount > 0)
     {
+      std::stringstream shaderName;
+      shaderName << "N_PATCH_" << xStretchCount << "x" << yStretchCount;
+
       std::stringstream vertexShader;
       vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
                    << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
                    << SHADER_NPATCH_VISUAL_SHADER_VERT;
 
-      shader = Shader::New(vertexShader.str(), fragmentShader);
+      shader = Shader::New(vertexShader.str(), fragmentShader, Dali::Shader::Hint::NONE, shaderName.str());
     }
   }
   else
@@ -474,12 +468,15 @@ Shader NPatchVisual::CreateShader()
     }
     else if(xStretchCount > 0 || yStretchCount > 0)
     {
+      std::stringstream shaderName;
+      shaderName << "N_PATCH_" << xStretchCount << "x" << yStretchCount;
+
       std::stringstream vertexShader;
       vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
                    << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
                    << SHADER_NPATCH_VISUAL_SHADER_VERT;
 
-      shader = Shader::New(vertexShader.str(), fragmentShader, hints);
+      shader = Shader::New(vertexShader.str(), fragmentShader, hints, shaderName.str());
     }
   }
 
@@ -488,13 +485,13 @@ Shader NPatchVisual::CreateShader()
 
 void NPatchVisual::ApplyTextureAndUniforms()
 {
-  std::shared_ptr<const NPatchData> data;
-  TextureSet                        textureSet;
+  NPatchDataPtr data;
+  TextureSet    textureSet;
 
   if(mLoader.GetNPatchData(mId, data) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE)
   {
     textureSet = data->GetTextures();
-    NPatchHelper::ApplyTextureAndUniforms(mImpl->mRenderer, data);
+    NPatchHelper::ApplyTextureAndUniforms(mImpl->mRenderer, data.Get());
 
     if(mAuxiliaryResourceStatus == Toolkit::Visual::ResourceStatus::READY)
     {
@@ -505,8 +502,8 @@ void NPatchVisual::ApplyTextureAndUniforms()
       // If we call textureSet.SetTexture(1, texture) directly, the cached TextureSet also be changed.
       // We should make pass utc-Dali-VisualFactory.cpp UtcDaliNPatchVisualAuxiliaryImage02().
       TextureSet tempTextureSet = TextureSet::New();
-      tempTextureSet.SetTexture(0, textureSet.GetTexture(0));
       tempTextureSet.SetTexture(1, mAuxiliaryTextureSet.GetTexture(0));
+      tempTextureSet.SetTexture(0, textureSet.GetTexture(0));
       textureSet = tempTextureSet;
 
       mImpl->mRenderer.RegisterProperty(DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA,
@@ -551,7 +548,7 @@ Geometry NPatchVisual::GetNinePatchGeometry(VisualFactoryCache::GeometryType sub
 
 void NPatchVisual::SetResource()
 {
-  std::shared_ptr<const NPatchData> data;
+  NPatchDataPtr data;
   if(mImpl->mRenderer && mLoader.GetNPatchData(mId, data))
   {
     Geometry geometry = CreateGeometry();
@@ -609,14 +606,7 @@ void NPatchVisual::LoadComplete(bool loadSuccess, TextureInformation textureInfo
     }
     if(loadSuccess)
     {
-      mAuxiliaryTextureSet = textureInformation.textureSet;
-      if(mAuxiliaryTextureSet)
-      {
-        Sampler sampler = Sampler::New();
-        sampler.SetWrapMode(WrapMode::DEFAULT, WrapMode::DEFAULT);
-        mAuxiliaryTextureSet.SetSampler(0u, sampler);
-      }
-
+      mAuxiliaryTextureSet     = textureInformation.textureSet;
       mAuxiliaryResourceStatus = Toolkit::Visual::ResourceStatus::READY;
     }
     else
@@ -628,7 +618,7 @@ void NPatchVisual::LoadComplete(bool loadSuccess, TextureInformation textureInfo
   // If auxiliaryUrl didn't required OR auxiliaryUrl load done.
   if(!mAuxiliaryUrl.IsValid() || mAuxiliaryResourceStatus != Toolkit::Visual::ResourceStatus::PREPARING)
   {
-    std::shared_ptr<const NPatchData> data;
+    NPatchDataPtr data;
     // and.. If Url loading done.
     if(mImpl->mRenderer && mLoader.GetNPatchData(mId, data) && data->GetLoadingState() != NPatchData::LoadingState::LOADING)
     {