Refactoring ImageVisualShaderFactory::GetShader
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / image-visual-shader-factory.cpp
index 7f2cac7..f5753fa 100644 (file)
@@ -18,6 +18,7 @@
 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
 
 // EXTERNAL INCLUDES
+#include <dali/devel-api/rendering/texture-devel.h>
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
@@ -40,9 +41,41 @@ static std::string gVertexShader;
 // global string variable to caching complate fragment shader (no atlas)
 static std::string gFragmentShaderNoAtlas;
 
+const int NATIVE_SHADER_TYPE_OFFSET = VisualFactoryCache::ShaderType::NATIVE_IMAGE_SHADER - VisualFactoryCache::ShaderType::IMAGE_SHADER;
+
 } // unnamed namespace
 
+namespace ImageVisualShaderFeature
+{
+FeatureBuilder& FeatureBuilder::EnableTextureAtlas(bool enableAtlas)
+{
+  mTextureAtlas = (enableAtlas ? TextureAtlas::ENABLED : TextureAtlas::DISABLED);
+  return *this;
+}
+FeatureBuilder& FeatureBuilder::ApplyDefaultTextureWrapMode(bool applyDefaultTextureWrapMode)
+{
+  mDefaultTextureWrapMode = (applyDefaultTextureWrapMode ? DefaultTextureWrapMode::APPLY : DefaultTextureWrapMode::DO_NOT_APPLY);
+  return *this;
+}
+FeatureBuilder& FeatureBuilder::EnableRoundedCorner(bool enableRoundedCorner)
+{
+  mRoundedCorner = (enableRoundedCorner ? RoundedCorner::ENABLED : RoundedCorner::DISABLED);
+  return *this;
+}
+FeatureBuilder& FeatureBuilder::EnableBorderline(bool enableBorderline)
+{
+  mBorderline = (enableBorderline ? Borderline::ENABLED : Borderline::DISABLED);
+  return *this;
+}
+FeatureBuilder& FeatureBuilder::SetTextureForFragmentShaderCheck(const Dali::Texture& texture)
+{
+  mTexture = texture;
+  return *this;
+}
+} // namespace ImageVisualShaderFeature
+
 ImageVisualShaderFactory::ImageVisualShaderFactory()
+: mFragmentShaderNeedChange(ImageVisualShaderFeature::ChangeFragmentShader::UNDECIDED)
 {
 }
 
@@ -50,13 +83,22 @@ ImageVisualShaderFactory::~ImageVisualShaderFactory()
 {
 }
 
-Shader ImageVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, TextureAtlas atlasing, DefaultTextureWrapMode defaultTextureWrapping, RoundedCorner roundedCorner, Borderline borderline)
+Shader ImageVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, const ImageVisualShaderFeature::FeatureBuilder& featureBuilder)
 {
   Shader shader;
   VisualFactoryCache::ShaderType shaderType = VisualFactoryCache::IMAGE_SHADER;
-  if(atlasing == TextureAtlas::ENABLED)
+
+  const auto& atlasing               = featureBuilder.mTextureAtlas;
+  const auto& defaultTextureWrapping = featureBuilder.mDefaultTextureWrapMode;
+  const auto& roundedCorner          = featureBuilder.mRoundedCorner;
+  const auto& borderline             = featureBuilder.mBorderline;
+  const auto& changeFragmentShader   = (featureBuilder.mTexture && DevelTexture::IsNative(featureBuilder.mTexture))
+                                       ? ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE
+                                       : ImageVisualShaderFeature::ChangeFragmentShader::DONT_CHANGE;
+
+  if(atlasing == ImageVisualShaderFeature::TextureAtlas::ENABLED)
   {
-    if(defaultTextureWrapping == DefaultTextureWrapMode::APPLY)
+    if(defaultTextureWrapping == ImageVisualShaderFeature::DefaultTextureWrapMode::APPLY)
     {
       shaderType = VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP;
     }
@@ -67,9 +109,9 @@ Shader ImageVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, Tex
   }
   else
   {
-    if(roundedCorner == RoundedCorner::ENABLED)
+    if(roundedCorner == ImageVisualShaderFeature::RoundedCorner::ENABLED)
     {
-      if(borderline == Borderline::ENABLED)
+      if(borderline == ImageVisualShaderFeature::Borderline::ENABLED)
       {
         shaderType = VisualFactoryCache::IMAGE_SHADER_ROUNDED_BORDERLINE;
       }
@@ -80,21 +122,28 @@ Shader ImageVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, Tex
     }
     else
     {
-      if(borderline == Borderline::ENABLED)
+      if(borderline == ImageVisualShaderFeature::Borderline::ENABLED)
       {
         shaderType = VisualFactoryCache::IMAGE_SHADER_BORDERLINE;
       }
     }
   }
 
+  if(changeFragmentShader == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE &&
+     (mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::UNDECIDED ||
+      mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE))
+  {
+    shaderType = static_cast<VisualFactoryCache::ShaderType>(static_cast<int>(shaderType) + NATIVE_SHADER_TYPE_OFFSET);
+  }
+
   shader = factoryCache.GetShader(shaderType);
   if(!shader)
   {
     std::string vertexShaderPrefixList;
     std::string fragmentShaderPrefixList;
-    if(atlasing == TextureAtlas::ENABLED)
+    if(atlasing == ImageVisualShaderFeature::TextureAtlas::ENABLED)
     {
-      if(defaultTextureWrapping == DefaultTextureWrapMode::APPLY)
+      if(defaultTextureWrapping == ImageVisualShaderFeature::DefaultTextureWrapMode::APPLY)
       {
         fragmentShaderPrefixList += "#define ATLAS_DEFAULT_WARP 1\n";
       }
@@ -105,20 +154,70 @@ Shader ImageVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, Tex
     }
     else
     {
-      if(roundedCorner == RoundedCorner::ENABLED)
+      if(roundedCorner == ImageVisualShaderFeature::RoundedCorner::ENABLED)
       {
         vertexShaderPrefixList   += "#define IS_REQUIRED_ROUNDED_CORNER 1\n";
         fragmentShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER 1\n";
       }
-      if(borderline == Borderline::ENABLED)
+      if(borderline == ImageVisualShaderFeature::Borderline::ENABLED)
       {
         vertexShaderPrefixList   += "#define IS_REQUIRED_BORDERLINE 1\n";
         fragmentShaderPrefixList += "#define IS_REQUIRED_BORDERLINE 1\n";
       }
     }
 
-    shader = Shader::New(Dali::Shader::GetVertexShaderPrefix()   + vertexShaderPrefixList   + SHADER_IMAGE_VISUAL_SHADER_VERT.data(),
-                         Dali::Shader::GetFragmentShaderPrefix() + fragmentShaderPrefixList + SHADER_IMAGE_VISUAL_SHADER_FRAG.data());
+    std::string vertexShader   = std::string(Dali::Shader::GetVertexShaderPrefix()   + vertexShaderPrefixList   + SHADER_IMAGE_VISUAL_SHADER_VERT.data());
+    std::string fragmentShader = std::string(Dali::Shader::GetFragmentShaderPrefix() + fragmentShaderPrefixList + SHADER_IMAGE_VISUAL_SHADER_FRAG.data());
+
+    if(changeFragmentShader == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE)
+    {
+      if(DALI_UNLIKELY(mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::UNDECIDED))
+      {
+        // NOTE : This routine will run exist one times.
+        //
+        // First, we will run ApplyNativeFragmentShader
+        //  - If fragment shader is modified, then current platform allow to change fragment shader.
+        //    We cache this result mFragmentShaderNeedChange = ChangeFragmentShader::NEED_CHANGE.
+        //  - If fragment shader is not modified, then current platform will always don't change fragment shader.
+        //    We cache this result mFragmentShaderNeedChange = ChangeFragmentShader::DONT_CHANGE.
+        //    And change current shaderType into normal image range.
+        //    After cached the result, shaderType never become NATIVE_IMAGE_SHADER anymore.
+        // Second, save shader result.
+
+        // Try to apply fragmentShader
+        bool modified = DevelTexture::ApplyNativeFragmentShader(featureBuilder.mTexture, fragmentShader);
+        if(modified)
+        {
+          // Now we know that fragment shader need to change.
+          mFragmentShaderNeedChange = ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE;
+        }
+        else
+        {
+          // Now we know that fragment shader even don't need to change.
+          // We can skip ApplyNativeFragmentShader routine after now.
+          mFragmentShaderNeedChange = ImageVisualShaderFeature::ChangeFragmentShader::DONT_CHANGE;
+
+          // Now we need normal shader type
+          // So decrease NATIVE_SHADER_TYPE_OFFSET.
+          shaderType = static_cast<VisualFactoryCache::ShaderType>(static_cast<int>(shaderType) - NATIVE_SHADER_TYPE_OFFSET);
+
+          // If we already compiled this type already, just use that cached shader.
+          // Else, just go forward.
+          shader = factoryCache.GetShader(shaderType);
+          if(shader)
+          {
+            return shader;
+          }
+        }
+      }
+      else if(mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE)
+      {
+        // Always need to apply fragmentShader
+        bool modified = DevelTexture::ApplyNativeFragmentShader(featureBuilder.mTexture, fragmentShader);
+        DALI_ASSERT_ALWAYS(modified && "NativeImageTexture need to change fragment shader. But DALI default image shader doesn't changed!");
+      }
+    }
+    shader = Shader::New(vertexShader, fragmentShader);
     shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
     factoryCache.SaveShader(shaderType, shader);
   }