From 108a91b70f76ed395aaff1694010c56f3d1a653c Mon Sep 17 00:00:00 2001 From: Heeyong Song Date: Thu, 20 Sep 2018 11:21:08 +0900 Subject: [PATCH] Fix a custom shader issue in ImageView A custom shader is ignored if a custom shader is set while loading a new image Change-Id: I2ffd994376d12488b3af9a73c3a8616514e082a0 --- .../src/dali-toolkit/utc-Dali-ImageView.cpp | 182 +++++++++++++++++++++ .../controls/image-view/image-view-impl.cpp | 48 ++++-- .../internal/controls/image-view/image-view-impl.h | 2 + 3 files changed, 219 insertions(+), 13 deletions(-) diff --git a/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp b/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp index 77121d5..98ff461 100644 --- a/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp +++ b/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp @@ -1775,3 +1775,185 @@ int UtcDaliImageViewFillMode(void) 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; +} diff --git a/dali-toolkit/internal/controls/image-view/image-view-impl.cpp b/dali-toolkit/internal/controls/image-view/image-view-impl.cpp index 6725bfb..5e520d8 100755 --- a/dali-toolkit/internal/controls/image-view/image-view-impl.cpp +++ b/dali-toolkit/internal/controls/image-view/image-view-impl.cpp @@ -64,7 +64,8 @@ DALI_TYPE_REGISTRATION_END() 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; } - 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 { @@ -139,6 +146,12 @@ void ImageView::SetImage( const Property::Map& map ) mVisual = visual; } + if( !mShaderMap.Empty() ) + { + Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual ); + visualImpl.SetCustomShader( mShaderMap ); + } + 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; + mImageSize = size; mImage.Reset(); mPropertyMap.Clear(); @@ -169,6 +183,12 @@ void ImageView::SetImage( const std::string& url, ImageDimensions size ) mVisual = visual; } + if( !mShaderMap.Empty() ) + { + Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual ); + visualImpl.SetCustomShader( mShaderMap ); + } + 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 - 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 ); } } } diff --git a/dali-toolkit/internal/controls/image-view/image-view-impl.h b/dali-toolkit/internal/controls/image-view/image-view-impl.h index 32867bf..4f05648 100644 --- a/dali-toolkit/internal/controls/image-view/image-view-impl.h +++ b/dali-toolkit/internal/controls/image-view/image-view-impl.h @@ -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 + Property::Map mShaderMap; ///< the Property::Map if the custom shader is set, empty otherwise + ImageDimensions mImageSize; ///< the image size }; } // namespace Internal -- 2.7.4