Fix a custom shader issue in ImageView 08/189708/4
authorHeeyong Song <heeyong.song@samsung.com>
Thu, 20 Sep 2018 02:21:08 +0000 (11:21 +0900)
committerHeeyong Song <heeyong.song@samsung.com>
Fri, 28 Sep 2018 09:20:10 +0000 (18:20 +0900)
A custom shader is ignored if a custom shader is set while loading a new image

Change-Id: I2ffd994376d12488b3af9a73c3a8616514e082a0

automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp
dali-toolkit/internal/controls/image-view/image-view-impl.cpp
dali-toolkit/internal/controls/image-view/image-view-impl.h

index 77121d5..98ff461 100644 (file)
@@ -1775,3 +1775,185 @@ int UtcDaliImageViewFillMode(void)
 
   END_TEST;
 }
 
   END_TEST;
 }
+
+int UtcDaliImageViewCustomShader(void)
+{
+  ToolkitTestApplication application;
+
+  // Set a custom shader with an image url
+  {
+    Property::Map properties;
+    Property::Map shader;
+    const std::string vertexShader = "Foobar";
+    const std::string fragmentShader = "Foobar";
+    shader[Visual::Shader::Property::FRAGMENT_SHADER] = fragmentShader;
+    shader[Visual::Shader::Property::VERTEX_SHADER] = vertexShader;
+
+    properties[Visual::Property::TYPE] = Visual::IMAGE;
+    properties[Visual::Property::SHADER] = shader;
+    properties[ImageVisual::Property::URL] = TEST_IMAGE_FILE_NAME;
+
+    ImageView imageView = ImageView::New();
+    imageView.SetProperty( ImageView::Property::IMAGE, properties );
+
+    Stage::GetCurrent().Add( imageView );
+
+    application.SendNotification();
+    application.Render();
+
+    DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
+
+    Renderer renderer = imageView.GetRendererAt( 0 );
+    Shader shader2 = renderer.GetShader();
+    Property::Value value = shader2.GetProperty( Shader::Property::PROGRAM );
+    Property::Map* map = value.GetMap();
+    DALI_TEST_CHECK( map );
+
+    Property::Value* fragment = map->Find( "fragment" ); // fragment key name from shader-impl.cpp
+    DALI_TEST_EQUALS( fragmentShader, fragment->Get< std::string >(), TEST_LOCATION );
+
+    Property::Value* vertex = map->Find( "vertex" ); // vertex key name from shader-impl.cpp
+    DALI_TEST_EQUALS( vertexShader, vertex->Get< std::string >(), TEST_LOCATION );
+  }
+
+  // Set a custom shader after setting an image url
+  {
+    Property::Map properties;
+    Property::Map shader;
+    const std::string vertexShader = "Foobar";
+    const std::string fragmentShader = "Foobar";
+    shader[Visual::Shader::Property::FRAGMENT_SHADER] = fragmentShader;
+    shader[Visual::Shader::Property::VERTEX_SHADER] = vertexShader;
+
+    properties[Visual::Property::SHADER] = shader;
+
+    ImageView imageView = ImageView::New( TEST_IMAGE_FILE_NAME );
+    imageView.SetProperty( ImageView::Property::IMAGE, properties );
+
+    Stage::GetCurrent().Add( imageView );
+
+    application.SendNotification();
+    application.Render();
+
+    Renderer renderer = imageView.GetRendererAt( 0 );
+    Shader shader2 = renderer.GetShader();
+    Property::Value value = shader2.GetProperty( Shader::Property::PROGRAM );
+    Property::Map* map = value.GetMap();
+    DALI_TEST_CHECK( map );
+
+    Property::Value* fragment = map->Find( "fragment" ); // fragment key name from shader-impl.cpp
+    DALI_TEST_EQUALS( fragmentShader, fragment->Get< std::string >(), TEST_LOCATION );
+
+    Property::Value* vertex = map->Find( "vertex" ); // vertex key name from shader-impl.cpp
+    DALI_TEST_EQUALS( vertexShader, vertex->Get< std::string >(), TEST_LOCATION );
+  }
+
+  // Set a custom shader before setting an image url
+  {
+    Property::Map properties;
+    Property::Map shader;
+    const std::string vertexShader = "Foobar";
+    const std::string fragmentShader = "Foobar";
+    shader[Visual::Shader::Property::FRAGMENT_SHADER] = fragmentShader;
+    shader[Visual::Shader::Property::VERTEX_SHADER] = vertexShader;
+
+    properties[Visual::Property::SHADER] = shader;
+
+    ImageView imageView = ImageView::New();
+    imageView.SetProperty( ImageView::Property::IMAGE, properties );
+    imageView.SetProperty( ImageView::Property::IMAGE, TEST_IMAGE_FILE_NAME );
+
+    Stage::GetCurrent().Add( imageView );
+
+    application.SendNotification();
+    application.Render();
+
+    Renderer renderer = imageView.GetRendererAt( 0 );
+    Shader shader2 = renderer.GetShader();
+    Property::Value value = shader2.GetProperty( Shader::Property::PROGRAM );
+    Property::Map* map = value.GetMap();
+    DALI_TEST_CHECK( map );
+
+    Property::Value* fragment = map->Find( "fragment" ); // fragment key name from shader-impl.cpp
+    DALI_TEST_EQUALS( fragmentShader, fragment->Get< std::string >(), TEST_LOCATION );
+
+    Property::Value* vertex = map->Find( "vertex" ); // vertex key name from shader-impl.cpp
+    DALI_TEST_EQUALS( vertexShader, vertex->Get< std::string >(), TEST_LOCATION );
+  }
+
+  // Set a custom shader after setting a property map
+  {
+    Property::Map properties;
+    Property::Map shader;
+    const std::string vertexShader = "Foobar";
+    const std::string fragmentShader = "Foobar";
+    shader[Visual::Shader::Property::FRAGMENT_SHADER] = fragmentShader;
+    shader[Visual::Shader::Property::VERTEX_SHADER] = vertexShader;
+
+    properties[Visual::Property::SHADER] = shader;
+
+    Property::Map properties1;
+    properties1[Visual::Property::TYPE] = Visual::IMAGE;
+    properties1[ImageVisual::Property::URL] = TEST_IMAGE_FILE_NAME;
+
+    ImageView imageView = ImageView::New();
+    imageView.SetProperty( ImageView::Property::IMAGE, properties1 );
+    imageView.SetProperty( ImageView::Property::IMAGE, properties );
+
+    Stage::GetCurrent().Add( imageView );
+
+    application.SendNotification();
+    application.Render();
+
+    Renderer renderer = imageView.GetRendererAt( 0 );
+    Shader shader2 = renderer.GetShader();
+    Property::Value value = shader2.GetProperty( Shader::Property::PROGRAM );
+    Property::Map* map = value.GetMap();
+    DALI_TEST_CHECK( map );
+
+    Property::Value* fragment = map->Find( "fragment" ); // fragment key name from shader-impl.cpp
+    DALI_TEST_EQUALS( fragmentShader, fragment->Get< std::string >(), TEST_LOCATION );
+
+    Property::Value* vertex = map->Find( "vertex" ); // vertex key name from shader-impl.cpp
+    DALI_TEST_EQUALS( vertexShader, vertex->Get< std::string >(), TEST_LOCATION );
+  }
+
+  // Set a custom shader before setting a property map
+  {
+    Property::Map properties;
+    Property::Map shader;
+    const std::string vertexShader = "Foobar";
+    const std::string fragmentShader = "Foobar";
+    shader[Visual::Shader::Property::FRAGMENT_SHADER] = fragmentShader;
+    shader[Visual::Shader::Property::VERTEX_SHADER] = vertexShader;
+
+    properties[Visual::Property::SHADER] = shader;
+
+    Property::Map properties1;
+    properties1[Visual::Property::TYPE] = Visual::IMAGE;
+    properties1[ImageVisual::Property::URL] = TEST_IMAGE_FILE_NAME;
+
+    ImageView imageView = ImageView::New();
+    imageView.SetProperty( ImageView::Property::IMAGE, properties );
+    imageView.SetProperty( ImageView::Property::IMAGE, properties1 );
+
+    Stage::GetCurrent().Add( imageView );
+
+    application.SendNotification();
+    application.Render();
+
+    Renderer renderer = imageView.GetRendererAt( 0 );
+    Shader shader2 = renderer.GetShader();
+    Property::Value value = shader2.GetProperty( Shader::Property::PROGRAM );
+    Property::Map* map = value.GetMap();
+    DALI_TEST_CHECK( map );
+
+    Property::Value* fragment = map->Find( "fragment" ); // fragment key name from shader-impl.cpp
+    DALI_TEST_EQUALS( fragmentShader, fragment->Get< std::string >(), TEST_LOCATION );
+
+    Property::Value* vertex = map->Find( "vertex" ); // vertex key name from shader-impl.cpp
+    DALI_TEST_EQUALS( vertexShader, vertex->Get< std::string >(), TEST_LOCATION );
+  }
+
+  END_TEST;
+}
index 6725bfb..5e520d8 100755 (executable)
@@ -64,7 +64,8 @@ DALI_TYPE_REGISTRATION_END()
 using namespace Dali;
 
 ImageView::ImageView()
 using namespace Dali;
 
 ImageView::ImageView()
-: Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) )
+: Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
+  mImageSize()
 {
 }
 
 {
 }
 
@@ -109,7 +110,13 @@ void ImageView::SetImage( Image image )
       mVisual = visual;
     }
 
       mVisual = visual;
     }
 
-    DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual  );
+    if( !mShaderMap.Empty() )
+    {
+      Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
+      visualImpl.SetCustomShader( mShaderMap );
+    }
+
+    DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
   }
   else
   {
   }
   else
   {
@@ -139,6 +146,12 @@ void ImageView::SetImage( const Property::Map& map )
       mVisual = visual;
     }
 
       mVisual = visual;
     }
 
+    if( !mShaderMap.Empty() )
+    {
+      Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
+      visualImpl.SetCustomShader( mShaderMap );
+    }
+
     DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual  );
   }
   else
     DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual  );
   }
   else
@@ -157,6 +170,7 @@ void ImageView::SetImage( const std::string& url, ImageDimensions size )
 {
   // Don't bother comparing if we had a visual previously, just drop old visual and create new one
   mUrl = url;
 {
   // Don't bother comparing if we had a visual previously, just drop old visual and create new one
   mUrl = url;
+  mImageSize = size;
   mImage.Reset();
   mPropertyMap.Clear();
 
   mImage.Reset();
   mPropertyMap.Clear();
 
@@ -169,6 +183,12 @@ void ImageView::SetImage( const std::string& url, ImageDimensions size )
       mVisual = visual;
     }
 
       mVisual = visual;
     }
 
+    if( !mShaderMap.Empty() )
+    {
+      Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
+      visualImpl.SetCustomShader( mShaderMap );
+    }
+
     DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
   }
   else
     DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
   }
   else
@@ -372,19 +392,21 @@ void ImageView::SetProperty( BaseObject* object, Property::Index index, const Pr
               impl.SetImage( *map );
             }
             // the property map contains only the custom shader
               impl.SetImage( *map );
             }
             // the property map contains only the custom shader
-            else if( ( impl.mVisual )&&( map->Count() == 1u )&&( shaderValue ) )
+            else if( ( map->Count() == 1u )&&( shaderValue ) )
             {
             {
-              Property::Map* shaderMap = shaderValue->GetMap();
-              if( shaderMap )
+              impl.mShaderMap = *( shaderValue->GetMap() );
+
+              if( !impl.mUrl.empty() )
+              {
+                impl.SetImage( impl.mUrl, impl.mImageSize );
+              }
+              else if( impl.mImage )
+              {
+                impl.SetImage( impl.mImage );
+              }
+              else if( !impl.mPropertyMap.Empty() )
               {
               {
-                Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
-                visual.SetCustomShader( *shaderMap );
-                if( imageView.OnStage() )
-                {
-                  // force to create new core renderer to use the newly set shader
-                  visual.SetOffStage( imageView );
-                  visual.SetOnStage( imageView );
-                }
+                impl.SetImage( impl.mPropertyMap );
               }
             }
           }
               }
             }
           }
index 32867bf..4f05648 100644 (file)
@@ -166,6 +166,8 @@ private:
   std::string      mUrl;          ///< the url for the image if the image came from a URL, empty otherwise
   Image            mImage;        ///< the Image if the image came from a Image, null otherwise
   Property::Map    mPropertyMap;  ///< the Property::Map if the image came from a Property::Map, empty otherwise
   std::string      mUrl;          ///< the url for the image if the image came from a URL, empty otherwise
   Image            mImage;        ///< the Image if the image came from a Image, null otherwise
   Property::Map    mPropertyMap;  ///< the Property::Map if the image came from a Property::Map, empty otherwise
+  Property::Map    mShaderMap;    ///< the Property::Map if the custom shader is set, empty otherwise
+  ImageDimensions  mImageSize;    ///< the image size
 };
 
 } // namespace Internal
 };
 
 } // namespace Internal