[Tizen] Set custom fragment shader to the visual used in WebView 59/258259/1
authorJiyun Yang <ji.yang@samsung.com>
Thu, 13 May 2021 03:14:47 +0000 (12:14 +0900)
committerJiyun Yang <ji.yang@samsung.com>
Thu, 13 May 2021 03:15:12 +0000 (12:15 +0900)
Change-Id: I87be6fe6356783ed25c55d6cee53f673ecb93e32
Signed-off-by: Jiyun Yang <ji.yang@samsung.com>
dali-toolkit/internal/controls/web-view/web-view-impl.cpp

index 6a3af8d..f98ea00 100644 (file)
@@ -109,7 +109,52 @@ DALI_TYPE_REGISTRATION_END()
 
 const std::string kEmptyString;
 
-} // namespace
+const char* DEFAULT_SAMPLER_TYPENAME = "sampler2D";
+
+const char* FRAGMENT_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
+  varying mediump vec2 vTexCoord;\n
+  uniform sampler2D sTexture;\n
+  uniform lowp vec4 uColor;\n
+  uniform lowp vec3 mixColor;\n
+  uniform lowp float preMultipliedAlpha;\n
+  \n
+  void main()\n
+  {\n
+      gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * vec4( mixColor, 1.0 );\n
+  }\n
+);
+
+Dali::Toolkit::Visual::Base CreateNativeImageVisual( NativeImageInterfacePtr nativeImageInterface )
+{
+  std::string fragmentShader;
+
+  const char* fragmentPrefix = nativeImageInterface->GetCustomFragmentPrefix();
+  if( fragmentPrefix )
+  {
+    fragmentShader = fragmentPrefix;
+    fragmentShader += FRAGMENT_SHADER_TEXTURE;
+  }
+  else
+  {
+    fragmentShader = FRAGMENT_SHADER_TEXTURE;
+  }
+
+  const char* customSamplerTypename = nativeImageInterface->GetCustomSamplerTypename();
+  if( customSamplerTypename )
+  {
+    fragmentShader.replace( fragmentShader.find( DEFAULT_SAMPLER_TYPENAME ), strlen( DEFAULT_SAMPLER_TYPENAME ), customSamplerTypename );
+  }
+
+  Texture texture = Dali::Texture::New( *nativeImageInterface );
+  const std::string nativeImageUrl = Dali::Toolkit::TextureManager::AddTexture( texture );
+
+  return Toolkit::VisualFactory::Get().CreateVisual(
+    { { Toolkit::Visual::Property::TYPE,  Toolkit::Visual::IMAGE } ,
+      { Toolkit::Visual::Property::SHADER, { { Toolkit::Visual::Shader::Property::FRAGMENT_SHADER, fragmentShader } } },
+      { Toolkit::ImageVisual::Property::URL, nativeImageUrl } } );
+}
+
+} // anonymous namepsace
 
 #define GET_ENUM_STRING(structName, inputExp) \
   Scripting::GetLinearEnumerationName<Toolkit::WebView::structName::Type>(static_cast<Toolkit::WebView::structName::Type>(inputExp), structName##_TABLE, structName##_TABLE_COUNT)
@@ -283,11 +328,7 @@ void WebView::LoadUrl(const std::string& url)
   mUrl = url;
   if(mWebEngine)
   {
-    Texture           texture        = Dali::Texture::New(*mWebEngine.GetNativeImageSource());
-    const std::string nativeImageUrl = Dali::Toolkit::TextureManager::AddTexture(texture);
-    mVisual                          = Toolkit::VisualFactory::Get().CreateVisual(
-      {{Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE},
-       {Toolkit::ImageVisual::Property::URL, nativeImageUrl}});
+    mVisual = CreateNativeImageVisual( mWebEngine.GetNativeImageSource() );
 
     if(mVisual)
     {
@@ -307,11 +348,7 @@ void WebView::LoadHtmlString(const std::string& htmlString)
 {
   if(mWebEngine)
   {
-    Texture           texture        = Dali::Texture::New(*mWebEngine.GetNativeImageSource());
-    const std::string nativeImageUrl = Dali::Toolkit::TextureManager::AddTexture(texture);
-    mVisual                          = Toolkit::VisualFactory::Get().CreateVisual(
-      {{Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE},
-       {Toolkit::ImageVisual::Property::URL, nativeImageUrl}});
+    mVisual = CreateNativeImageVisual( mWebEngine.GetNativeImageSource() );
 
     if(mVisual)
     {