From: Subhransu Mohanty Date: Fri, 4 Dec 2020 01:38:56 +0000 (+0900) Subject: Avoid shader string copy when possible. X-Git-Tag: dali_2.0.6~1^2 X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=commitdiff_plain;h=dbc35ad10b31e70a99ef60c23fb0f5248a8ed271 Avoid shader string copy when possible. 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 --- diff --git a/dali-toolkit/internal/visuals/image/image-visual.cpp b/dali-toolkit/internal/visuals/image/image-visual.cpp index 22a54e6..0cd4c1b 100644 --- a/dali-toolkit/internal/visuals/image/image-visual.cpp +++ b/dali-toolkit/internal/visuals/image/image-visual.cpp @@ -916,35 +916,38 @@ Shader ImageVisual::GetShader() { Shader shader; - std::string vertexShader; - bool usesWholeTexture = true; + std::string_view vertexShaderView; + bool usesWholeTexture = true; if(mImpl->mCustomShader && !mImpl->mCustomShader->mVertexShader.empty()) { - vertexShader = mImpl->mCustomShader->mVertexShader; + vertexShaderView = mImpl->mCustomShader->mVertexShader; 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()) { - fragmentShader = mImpl->mCustomShader->mFragmentShader; + fragmentShaderView = mImpl->mCustomShader->mFragmentShader; } 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; + std::string fragmentShaderString; 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; @@ -955,26 +958,26 @@ Shader ImageVisual::GetShader() mFactoryCache, mImpl->mFlags & Impl::IS_ATLASING_APPLIED, mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE, - IsRoundedCornerRequired()); + IsRoundedCornerRequired() ); } else if(mImpl->mCustomShader) { - shader = Shader::New(vertexShader, fragmentShader, mImpl->mCustomShader->mHints); + shader = Shader::New(vertexShaderView, fragmentShaderView, mImpl->mCustomShader->mHints); } else { - shader = Shader::New(vertexShader, fragmentShader); + shader = Shader::New(vertexShaderView, fragmentShaderView); } 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 - shader.RegisterProperty(PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF); + shader.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF ); return shader; }