Disable Image Visual Pixel alignment when using Layer3D
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / image / image-visual.cpp
index f6e2a9f..3c82d75 100644 (file)
 // CLASS HEADER
 #include <dali-toolkit/internal/visuals/image/image-visual.h>
 
-// EXTERNAL HEADER
-#include <cstring> // for strncasecmp
+// EXTERNAL HEADERS
+#include <cstring> // for strlen()
+#include <dali/public-api/actors/layer.h>
 #include <dali/public-api/images/resource-image.h>
 #include <dali/public-api/images/native-image.h>
 #include <dali/devel-api/images/texture-set-image.h>
 #include <dali/devel-api/adaptor-framework/bitmap-loader.h>
+#include <dali/devel-api/adaptor-framework/image-loading.h>
 #include <dali/devel-api/scripting/enum-helper.h>
 #include <dali/devel-api/scripting/scripting.h>
 #include <dali/integration-api/debug.h>
 
-// INTERNAL HEADER
+// INTERNAL HEADERS
 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
+#include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
@@ -37,6 +40,7 @@
 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
+#include <dali-toolkit/internal/visuals/visual-url.h>
 
 namespace Dali
 {
@@ -49,8 +53,6 @@ namespace Internal
 
 namespace
 {
-const char HTTP_URL[] = "http://";
-const char HTTPS_URL[] = "https://";
 
 // property names
 const char * const IMAGE_FITTING_MODE( "fittingMode" );
@@ -58,6 +60,7 @@ const char * const IMAGE_SAMPLING_MODE( "samplingMode" );
 const char * const IMAGE_DESIRED_WIDTH( "desiredWidth" );
 const char * const IMAGE_DESIRED_HEIGHT( "desiredHeight" );
 const char * const SYNCHRONOUS_LOADING( "synchronousLoading" );
+const char * const IMAGE_ATLASING("atlasing");
 
 // fitting modes
 DALI_ENUM_TO_STRING_TABLE_BEGIN( FITTING_MODE )
@@ -91,31 +94,39 @@ const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
 
 const char* DEFAULT_SAMPLER_TYPENAME = "sampler2D";
 
+const float PIXEL_ALIGN_ON = 1.0f;
+const float PIXEL_ALIGN_OFF = 0.0f;
+
 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
   attribute mediump vec2 aPosition;\n
-  uniform mediump mat4 uMvpMatrix;\n
+  uniform mediump mat4 uModelMatrix;\n
+  uniform mediump mat4 uViewMatrix;\n
+  uniform mediump mat4 uProjection;\n
   uniform mediump vec3 uSize;\n
   uniform mediump vec4 pixelArea;
   varying mediump vec2 vTexCoord;\n
+  uniform lowp float uPixelAligned;\n
   \n
-
   //Visual size and offset
   uniform mediump vec2 offset;\n
   uniform mediump vec2 size;\n
   uniform mediump vec4 offsetSizeMode;\n
   uniform mediump vec2 origin;\n
   uniform mediump vec2 anchorPoint;\n
-
+\n
   vec4 ComputeVertexPosition()\n
   {\n
     vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
     vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
     return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
   }\n
-
+\n
   void main()\n
   {\n
-    mediump vec4 vertexPosition = uMvpMatrix * ComputeVertexPosition();\n
+    mediump vec4 vertexPosition = uViewMatrix * uModelMatrix * ComputeVertexPosition();\n
+    vec4 alignedVertexPosition = vertexPosition;\n
+    alignedVertexPosition.xy = floor ( vertexPosition.xy );\n // Pixel alignment
+    vertexPosition = uProjection * mix( vertexPosition, alignedVertexPosition, uPixelAligned );\n
     vTexCoord = pixelArea.xy+pixelArea.zw*(aPosition + vec2(0.5) );\n
     gl_Position = vertexPosition;\n
   }\n
@@ -213,7 +224,7 @@ Geometry CreateGeometry( VisualFactoryCache& factoryCache, ImageDimensions gridS
 } // unnamed namespace
 
 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache,
-                                 const std::string& imageUrl,
+                                 const VisualUrl& imageUrl,
                                  const Property::Map& properties,
                                  ImageDimensions size,
                                  FittingMode::Type fittingMode,
@@ -225,7 +236,7 @@ ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache,
 }
 
 ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache,
-                                 const std::string& imageUrl,
+                                 const VisualUrl& imageUrl,
                                  ImageDimensions size,
                                  FittingMode::Type fittingMode,
                                  Dali::SamplingMode::Type samplingMode )
@@ -239,7 +250,7 @@ ImageVisualPtr ImageVisual::New( VisualFactoryCache& factoryCache, const Image&
 }
 
 ImageVisual::ImageVisual( VisualFactoryCache& factoryCache,
-                          const std::string& imageUrl,
+                          const VisualUrl& imageUrl,
                           ImageDimensions size,
                           FittingMode::Type fittingMode,
                           Dali::SamplingMode::Type samplingMode )
@@ -253,7 +264,8 @@ ImageVisual::ImageVisual( VisualFactoryCache& factoryCache,
   mFittingMode( fittingMode ),
   mSamplingMode( samplingMode ),
   mWrapModeU( WrapMode::DEFAULT ),
-  mWrapModeV( WrapMode::DEFAULT )
+  mWrapModeV( WrapMode::DEFAULT ),
+  mAttemptAtlasing( false )
 {
 }
 
@@ -268,7 +280,8 @@ ImageVisual::ImageVisual( VisualFactoryCache& factoryCache, const Image& image )
   mFittingMode( FittingMode::DEFAULT ),
   mSamplingMode( SamplingMode::DEFAULT ),
   mWrapModeU( WrapMode::DEFAULT ),
-  mWrapModeV( WrapMode::DEFAULT )
+  mWrapModeV( WrapMode::DEFAULT ),
+  mAttemptAtlasing( false )
 {
 }
 
@@ -320,10 +333,14 @@ void ImageVisual::DoSetProperties( const Property::Map& propertyMap )
       {
         DoSetProperty( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, keyValue.second );
       }
+      else if ( keyValue.first == IMAGE_ATLASING )
+      {
+        DoSetProperty( Toolkit::DevelImageVisual::Property::ATLASING, keyValue.second );
+      }
     }
   }
 
-  if( mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING && mImageUrl.size() > 0u )
+  if( ( mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING ) && mImageUrl.IsValid() )
   {
     // if sync loading is required, the loading should start
     // immediately when new image url is set or the actor is off stage
@@ -423,6 +440,12 @@ void ImageVisual::DoSetProperty( Property::Index index, const Property::Value& v
       mWrapModeV = Dali::WrapMode::Type( wrapMode );
       break;
     }
+
+    case Toolkit::DevelImageVisual::Property::ATLASING:
+    {
+      bool atlasing = false;
+      mAttemptAtlasing = value.Get( atlasing );
+    }
   }
 }
 
@@ -440,9 +463,9 @@ void ImageVisual::GetNaturalSize( Vector2& naturalSize )
     naturalSize.y = mDesiredSize.GetHeight();
     return;
   }
-  else if( !mImageUrl.empty() )
+  else if( mImageUrl.IsValid() && mImageUrl.GetLocation() == VisualUrl::LOCAL )
   {
-    ImageDimensions dimentions = ResourceImage::GetImageSize( mImageUrl );
+    ImageDimensions dimentions = Dali::GetClosestImageSize( mImageUrl.GetUrl() );
     naturalSize.x = dimentions.GetWidth();
     naturalSize.y = dimentions.GetHeight();
     return;
@@ -483,6 +506,8 @@ void ImageVisual::CreateRenderer( TextureSet& textures )
     }
   }
 
+  shader.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_ON ); // Set default to align
+
   mImpl->mRenderer = Renderer::New( geometry, shader );
   DALI_ASSERT_DEBUG( textures );
   mImpl->mRenderer.SetTextures( textures );
@@ -499,11 +524,13 @@ void ImageVisual::CreateNativeImageRenderer( NativeImage& nativeImage )
   std::string fragmentShader;
   const char* fragmentPreFix = nativeImage.GetCustomFragmentPreFix();
   const char* customSamplerTypename = nativeImage.GetCustomSamplerTypename();
+
   if( fragmentPreFix )
   {
     fragmentShader = fragmentPreFix;
     fragmentShader += "\n";
   }
+
   if( mImpl->mCustomShader && !mImpl->mCustomShader->mFragmentShader.empty() )
   {
     fragmentShader += mImpl->mCustomShader->mFragmentShader;
@@ -512,6 +539,7 @@ void ImageVisual::CreateNativeImageRenderer( NativeImage& nativeImage )
   {
     fragmentShader += FRAGMENT_SHADER_NO_ATLAS;
   }
+
   if( customSamplerTypename )
   {
     fragmentShader.replace( fragmentShader.find( DEFAULT_SAMPLER_TYPENAME ), strlen( DEFAULT_SAMPLER_TYPENAME ), customSamplerTypename );
@@ -550,15 +578,15 @@ bool ImageVisual::IsSynchronousResourceLoading() const
 
 void ImageVisual::LoadResourceSynchronously()
 {
-  if( !mImageUrl.empty() )
+  if( mImageUrl.IsValid() )
   {
-    BitmapLoader loader = BitmapLoader::New( mImageUrl, mDesiredSize, mFittingMode, mSamplingMode );
+    BitmapLoader loader = BitmapLoader::New( mImageUrl.GetUrl(), mDesiredSize, mFittingMode, mSamplingMode );
     loader.Load();
     mPixels = loader.GetPixelData();
   }
 }
 
-TextureSet ImageVisual::CreateTextureSet( Vector4& textureRect, const std::string& url, bool synchronousLoading, bool attemptAtlasing )
+TextureSet ImageVisual::CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing )
 {
   TextureSet textureSet;
   textureRect = FULL_TEXTURE_RECT;
@@ -592,13 +620,13 @@ TextureSet ImageVisual::CreateTextureSet( Vector4& textureRect, const std::strin
   {
     if( attemptAtlasing )
     {
-      textureSet = mFactoryCache.GetAtlasManager()->Add(textureRect, url, mDesiredSize, mFittingMode, true, this );
+      textureSet = mFactoryCache.GetAtlasManager()->Add( textureRect, mImageUrl.GetUrl(), mDesiredSize, mFittingMode, true, this );
       mImpl->mFlags |= Impl::IS_ATLASING_APPLIED;
     }
     if( !textureSet ) // big image, no atlasing or atlasing failed
     {
       mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
-      ResourceImage resourceImage = Dali::ResourceImage::New( url, mDesiredSize, mFittingMode, mSamplingMode );
+      ResourceImage resourceImage = Dali::ResourceImage::New( mImageUrl.GetUrl(), mDesiredSize, mFittingMode, mSamplingMode );
       resourceImage.LoadingFinishedSignal().Connect( this, &ImageVisual::OnImageLoaded );
       textureSet = TextureSet::New();
       TextureSetImage( textureSet, 0u, resourceImage );
@@ -614,74 +642,45 @@ TextureSet ImageVisual::CreateTextureSet( Vector4& textureRect, const std::strin
   return textureSet;
 }
 
-void ImageVisual::InitializeRenderer( const std::string& imageUrl )
+void ImageVisual::InitializeRenderer()
 {
-  mImageUrl = imageUrl;
   mImpl->mFlags &= ~Impl::IS_ATLASING_APPLIED;
 
-  if( !mImpl->mCustomShader &&
-      ( strncasecmp( imageUrl.c_str(), HTTP_URL,  sizeof(HTTP_URL)  -1 ) != 0 ) && // dont atlas remote images
-      ( strncasecmp( imageUrl.c_str(), HTTPS_URL, sizeof(HTTPS_URL) -1 ) != 0 ) )
+  if( !mImpl->mCustomShader && mImageUrl.GetLocation() == VisualUrl::LOCAL )
   {
     bool defaultWrapMode = mWrapModeU <= WrapMode::CLAMP_TO_EDGE && mWrapModeV <= WrapMode::CLAMP_TO_EDGE;
-    bool defaultTransform = mImpl->mTransform.mSize == Vector2::ONE &&
-                            mImpl->mTransform.mOffset == Vector2::ZERO &&
-                            mImpl->mTransform.mOffsetSizeMode == Vector4::ZERO &&
-                            mImpl->mTransform.mOrigin == Toolkit::Align::CENTER &&
-                            mImpl->mTransform.mAnchorPoint == Toolkit::Align::CENTER;
-
-    bool cacheable =  defaultWrapMode && defaultTransform &&  mPixelArea == FULL_TEXTURE_RECT;
 
+    Vector4 atlasRect;
+    // texture set has to be created first as we need to know if atlasing succeeded or not
+    // when selecting the shader
+    TextureSet textures = CreateTextureSet( atlasRect, IsSynchronousResourceLoading(), mAttemptAtlasing );
+    CreateRenderer( textures );
 
-    mImpl->mFlags &= ~Impl::IS_FROM_CACHE;
-    if( cacheable ) // fetch the renderer from cache if exist
-    {
-      mImpl->mRenderer = mFactoryCache.GetRenderer( imageUrl );
-      mImpl->mFlags |= Impl::IS_FROM_CACHE;
-    }
-
-    if( !mImpl->mRenderer ) // new renderer is needed
+    if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED ) // the texture is packed inside atlas
     {
-      Vector4 atlasRect;
-      // texture set has to be created first as we need to know if atlasing succeeded or not
-      // when selecting the shader
-      TextureSet textures = CreateTextureSet( atlasRect, imageUrl, IsSynchronousResourceLoading(), true );
-      CreateRenderer( textures );
-
-      if( mImpl->mFlags & Impl::IS_ATLASING_APPLIED ) // the texture is packed inside atlas
+      mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
+      if( !defaultWrapMode ) // custom wrap mode, renderer is not cached.
       {
-        mImpl->mRenderer.RegisterProperty( ATLAS_RECT_UNIFORM_NAME, atlasRect );
-        if( !defaultWrapMode ) // custom wrap mode, renderer is not cached.
-        {
-          Vector2 wrapMode(mWrapModeU-WrapMode::CLAMP_TO_EDGE, mWrapModeV-WrapMode::CLAMP_TO_EDGE);
-          wrapMode.Clamp( Vector2::ZERO, Vector2( 2.f, 2.f ) );
-          mImpl->mRenderer.RegisterProperty( WRAP_MODE_UNIFORM_NAME, wrapMode );
-        }
-      }
-
-      // save the renderer to cache only when default wrap mode and default pixel area is used
-      if( cacheable )
-      {
-        mFactoryCache.SaveRenderer( imageUrl, mImpl->mRenderer );
+        Vector2 wrapMode(mWrapModeU-WrapMode::CLAMP_TO_EDGE, mWrapModeV-WrapMode::CLAMP_TO_EDGE);
+        wrapMode.Clamp( Vector2::ZERO, Vector2( 2.f, 2.f ) );
+        mImpl->mRenderer.RegisterProperty( WRAP_MODE_UNIFORM_NAME, wrapMode );
       }
     }
   }
   else
   {
-    // for custom shader or remote image, renderer is not cached and atlas is not applied
-    mImpl->mFlags &= ~Impl::IS_FROM_CACHE;
+    // for custom shader or remote image, atlas is not applied
     Vector4 atlasRect; // ignored in this case
-    TextureSet textures = CreateTextureSet( atlasRect, imageUrl, IsSynchronousResourceLoading(), false );
+    TextureSet textures = CreateTextureSet( atlasRect, IsSynchronousResourceLoading(), false );
     CreateRenderer( textures );
   }
 }
 
 void ImageVisual::InitializeRenderer( const Image& image )
 {
-  mImpl->mFlags &= ~Impl::IS_FROM_CACHE;
-
   // don't reuse CreateTextureSet
   TextureSet textures = TextureSet::New();
+
   // Renderer can't be shared if mImage is NativeImage
   NativeImage nativeImage = NativeImage::DownCast( image );
   if( nativeImage )
@@ -712,9 +711,9 @@ void ImageVisual::UploadCompleted()
 
 void ImageVisual::DoSetOnStage( Actor& actor )
 {
-  if( !mImageUrl.empty() )
+  if( mImageUrl.IsValid() )
   {
-    InitializeRenderer( mImageUrl );
+    InitializeRenderer();
   }
   else if ( mImage )
   {
@@ -728,6 +727,14 @@ void ImageVisual::DoSetOnStage( Actor& actor )
 
   mPlacementActor = actor;
 
+  // Search the Actor tree to find if Layer UI behaviour set.
+  Layer layer = actor.GetLayer();
+  if ( layer && layer.GetBehavior() == Layer::LAYER_3D )
+  {
+     // Layer 3D set, do not align pixels
+     mImpl->mRenderer.RegisterProperty( PIXEL_ALIGNED_UNIFORM_NAME, PIXEL_ALIGN_OFF );
+  }
+
   if( mPixelArea != FULL_TEXTURE_RECT )
   {
     mImpl->mRenderer.RegisterProperty( PIXEL_AREA_UNIFORM_NAME, mPixelArea );
@@ -746,9 +753,9 @@ void ImageVisual::DoSetOffStage( Actor& actor )
 
   //If we own the image then make sure we release it when we go off stage
   actor.RemoveRenderer( mImpl->mRenderer);
-  if( !mImageUrl.empty() )
+  if( mImageUrl.IsValid() )
   {
-    CleanCache(mImageUrl);
+    RemoveFromAtlas( mImageUrl.GetUrl() );
     mImage.Reset();
   }
 
@@ -763,9 +770,9 @@ void ImageVisual::DoCreatePropertyMap( Property::Map& map ) const
 
   bool sync = IsSynchronousResourceLoading();
   map.Insert( SYNCHRONOUS_LOADING, sync );
-  if( !mImageUrl.empty() )
+  if( mImageUrl.IsValid() )
   {
-    map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl );
+    map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
     map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
     map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
   }
@@ -787,6 +794,24 @@ void ImageVisual::DoCreatePropertyMap( Property::Map& map ) const
   map.Insert( Toolkit::ImageVisual::Property::PIXEL_AREA, mPixelArea );
   map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_U, mWrapModeU );
   map.Insert( Toolkit::ImageVisual::Property::WRAP_MODE_V, mWrapModeV );
+
+  map.Insert( Toolkit::DevelImageVisual::Property::ATLASING, mAttemptAtlasing );
+}
+
+void ImageVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
+{
+  map.Clear();
+  map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::Visual::IMAGE );
+  if( mImageUrl.IsValid() )
+  {
+    map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, mDesiredSize.GetWidth() );
+    map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, mDesiredSize.GetHeight() );
+  }
+  else if( mImage )
+  {
+    map.Insert( Toolkit::ImageVisual::Property::DESIRED_WIDTH, static_cast<int>(mImage.GetWidth()) );
+    map.Insert( Toolkit::ImageVisual::Property::DESIRED_HEIGHT, static_cast<int>(mImage.GetHeight()) );
+  }
 }
 
 void ImageVisual::OnSetTransform()
@@ -863,24 +888,22 @@ void ImageVisual::OnImageLoaded( ResourceImage image )
   }
 }
 
-void ImageVisual::CleanCache(const std::string& url)
+void ImageVisual::RemoveFromAtlas(const std::string& url)
 {
-  if( IsFromCache() )
+  Vector4 atlasRect( 0.f, 0.f, 1.f, 1.f );
+  Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
+  if( index != Property::INVALID_INDEX )
   {
-    Vector4 atlasRect( 0.f, 0.f, 1.f, 1.f );
-    Property::Index index = mImpl->mRenderer.GetPropertyIndex( ATLAS_RECT_UNIFORM_NAME );
-    if( index != Property::INVALID_INDEX )
-    {
-      Property::Value atlasRectValue = mImpl->mRenderer.GetProperty( index );
-      atlasRectValue.Get( atlasRect );
-    }
+    Property::Value atlasRectValue = mImpl->mRenderer.GetProperty( index );
+    atlasRectValue.Get( atlasRect );
+  }
 
-    TextureSet textureSet = mImpl->mRenderer.GetTextures();
-    mImpl->mRenderer.Reset();
-    if( mFactoryCache.CleanRendererCache( url ) && index != Property::INVALID_INDEX )
-    {
-      mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
-    }
+  TextureSet textureSet = mImpl->mRenderer.GetTextures();
+  mImpl->mRenderer.Reset();
+
+  if( index != Property::INVALID_INDEX )
+  {
+    mFactoryCache.GetAtlasManager()->Remove( textureSet, atlasRect );
   }
 }