Avoid shader string copy when possible. 75/248975/3
authorSubhransu Mohanty <sub.mohanty@samsung.com>
Fri, 4 Dec 2020 01:38:56 +0000 (10:38 +0900)
committerDavid Steele <david.steele@samsung.com>
Wed, 16 Dec 2020 18:22:14 +0000 (18:22 +0000)
As shader creation will take a copy if the shader is not in the cache
we don't have to create new String for each shader creation request.

we create a new string only when we need to update the shader data
and only pass a string_view to the shader creation.

Change-Id: I595d5f2af8216c2526ba0865fe33b6c0c2538405

dali-toolkit/internal/visuals/image/image-visual.cpp

index 22a54e6..0cd4c1b 100644 (file)
@@ -916,35 +916,38 @@ Shader ImageVisual::GetShader()
 {
   Shader shader;
 
 {
   Shader shader;
 
-  std::string vertexShader;
-  bool        usesWholeTexture = true;
+  std::string_view vertexShaderView;
+  bool usesWholeTexture = true;
   if(mImpl->mCustomShader && !mImpl->mCustomShader->mVertexShader.empty())
   {
   if(mImpl->mCustomShader && !mImpl->mCustomShader->mVertexShader.empty())
   {
-    vertexShader     = mImpl->mCustomShader->mVertexShader;
+    vertexShaderView = mImpl->mCustomShader->mVertexShader;
     usesWholeTexture = false; // Impossible to tell.
   }
   else
   {
     usesWholeTexture = false; // Impossible to tell.
   }
   else
   {
-    vertexShader = mImageVisualShaderFactory.GetVertexShaderSource().data();
+    vertexShaderView = mImageVisualShaderFactory.GetVertexShaderSource();
   }
 
   }
 
-  std::string fragmentShader;
+  std::string_view fragmentShaderView;
   if(mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty())
   {
   if(mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty())
   {
-    fragmentShader = mImpl->mCustomShader->mFragmentShader;
+    fragmentShaderView = mImpl->mCustomShader->mFragmentShader;
   }
   else
   {
   }
   else
   {
-    fragmentShader = mImageVisualShaderFactory.GetFragmentShaderSource().data();
+    fragmentShaderView = mImageVisualShaderFactory.GetFragmentShaderSource();
   }
 
   // If the texture is native, we may need to change prefix and sampler in
   // the fragment shader
   bool modifiedFragmentShader = false;
   }
 
   // If the texture is native, we may need to change prefix and sampler in
   // the fragment shader
   bool modifiedFragmentShader = false;
+  std::string fragmentShaderString;
   if(mTextures && DevelTexture::IsNative(mTextures.GetTexture(0)))
   {
   if(mTextures && DevelTexture::IsNative(mTextures.GetTexture(0)))
   {
-    Texture nativeTexture  = mTextures.GetTexture(0);
-    modifiedFragmentShader = DevelTexture::ApplyNativeFragmentShader(nativeTexture, fragmentShader);
+    Texture nativeTexture = mTextures.GetTexture(0);
+    fragmentShaderString   = std::string(fragmentShaderView);
+    modifiedFragmentShader = DevelTexture::ApplyNativeFragmentShader(nativeTexture, fragmentShaderString);
+    fragmentShaderView     = fragmentShaderString;
   }
 
   const bool useStandardShader = !mImpl->mCustomShader && !modifiedFragmentShader;
   }
 
   const bool useStandardShader = !mImpl->mCustomShader && !modifiedFragmentShader;
@@ -955,26 +958,26 @@ Shader ImageVisual::GetShader()
       mFactoryCache,
       mImpl->mFlags & Impl::IS_ATLASING_APPLIED,
       mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE,
       mFactoryCache,
       mImpl->mFlags & Impl::IS_ATLASING_APPLIED,
       mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE,
-      IsRoundedCornerRequired());
+      IsRoundedCornerRequired() );
   }
   else if(mImpl->mCustomShader)
   {
   }
   else if(mImpl->mCustomShader)
   {
-    shader = Shader::New(vertexShader, fragmentShader, mImpl->mCustomShader->mHints);
+    shader = Shader::New(vertexShaderView, fragmentShaderView, mImpl->mCustomShader->mHints);
   }
   else
   {
   }
   else
   {
-    shader = Shader::New(vertexShader, fragmentShader);
+    shader = Shader::New(vertexShaderView, fragmentShaderView);
   }
 
   if(usesWholeTexture)
   {
   }
 
   if(usesWholeTexture)
   {
-    shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
+    shader.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT );
   }
 
   // Set pixel align off as default.
   // ToDo: Pixel align causes issues such as rattling image animation.
   // We should trun it off until issues are resolved
   }
 
   // Set pixel align off as default.
   // ToDo: Pixel align causes issues such as rattling image animation.
   // We should trun it off until issues are resolved
-  shader.RegisterProperty(PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF);
+  shader.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
 
   return shader;
 }
 
   return shader;
 }