[Tizen] Set custom fragment shader to the visual used in WebView
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / web-view / web-view-impl.cpp
index b0d81de..a24d6a6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 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.
 #include <cstring>
 #include <dali/devel-api/scripting/enum-helper.h>
 #include <dali/devel-api/scripting/scripting.h>
-#include <dali/public-api/common/stage.h>
-#include <dali/public-api/images/native-image.h>
+#include <dali/devel-api/common/stage.h>
 #include <dali/public-api/adaptor-framework/native-image-source.h>
 #include <dali/public-api/object/type-registry.h>
 #include <dali/public-api/object/type-registry-helper.h>
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/devel-api/controls/control-devel.h>
+#include <dali-toolkit/devel-api/image-loader/texture-manager.h>
 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
+#include <dali-toolkit/public-api/visuals/image-visual-properties.h>
 
 namespace Dali
 {
@@ -80,6 +81,51 @@ DALI_TYPE_REGISTRATION_END()
 
 const std::string kEmptyString;
 
+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 ) \
@@ -136,8 +182,8 @@ Toolkit::WebView WebView::New( const std::string& locale, const std::string& tim
 
 void WebView::OnInitialize()
 {
-  Self().SetKeyboardFocusable( true );
-  Self().TouchSignal().Connect( this, &WebView::OnTouchEvent );
+  Self().SetProperty( Actor::Property::KEYBOARD_FOCUSABLE, true );
+  Self().TouchedSignal().Connect( this, &WebView::OnTouchEvent );
 
   if( mWebEngine )
   {
@@ -152,8 +198,7 @@ void WebView::LoadUrl( const std::string& url )
   mUrl = url;
   if( mWebEngine )
   {
-    Dali::Image image = Dali::NativeImage::New( *mWebEngine.GetNativeImageSource() );
-    mVisual = Toolkit::VisualFactory::Get().CreateVisual( image );
+    mVisual = CreateNativeImageVisual( mWebEngine.GetNativeImageSource() );
 
     if( mVisual )
     {
@@ -168,8 +213,7 @@ void WebView::LoadHTMLString( const std::string& htmlString )
 {
   if( mWebEngine )
   {
-    Dali::Image image = Dali::NativeImage::New( *mWebEngine.GetNativeImageSource() );
-    mVisual = Toolkit::VisualFactory::Get().CreateVisual( image );
+    mVisual = CreateNativeImageVisual( mWebEngine.GetNativeImageSource() );
 
     if( mVisual )
     {
@@ -512,7 +556,7 @@ Property::Value WebView::GetProperty( BaseObject* object, Property::Index proper
   return value;
 }
 
-bool WebView::OnTouchEvent( Actor actor, const Dali::TouchData& touch )
+bool WebView::OnTouchEvent( Actor actor, const Dali::TouchEvent& touch )
 {
   bool result = false;