Fix a custom shader issue
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / npatch / npatch-visual.cpp
index 33a4842..6ae1d8e 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2018 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.
@@ -56,6 +56,7 @@ const char * const AUXILIARY_IMAGE_ALPHA_NAME( "auxiliaryImageAlpha" );
 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
   attribute mediump vec2 aPosition;\n
   varying mediump vec2 vTexCoord;\n
+  varying mediump vec2 vMaskTexCoord;\n
   uniform mediump mat4 uMvpMatrix;\n
   uniform mediump vec3 uSize;\n
   uniform mediump vec2 uNinePatchFactorsX[ FACTOR_SIZE_X ];\n
@@ -83,7 +84,7 @@ const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
     mediump vec4 gridPosition = vec4( fixedFactor + ( visualSize.xy - fixedTotal ) * stretch / stretchTotal, 0.0, 1.0 );\n
     mediump vec4 vertexPosition = gridPosition;\n
     vertexPosition.xy -= visualSize.xy * vec2( 0.5, 0.5 );\n
-    vertexPostion.xy += anchorPoint*visualSize + (visualOffset + origin)*uSize.xy;\n
+    vertexPosition.xy += anchorPoint*visualSize + (visualOffset + origin)*uSize.xy;\n
     vertexPosition = uMvpMatrix * vertexPosition;\n
     \n
     vTexCoord = ( fixedFactor + stretch ) / ( fixedTotal + stretchTotal );\n
@@ -140,15 +141,11 @@ const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
   uniform sampler2D sTexture;\n
   uniform lowp vec4 uColor;\n
   uniform lowp vec3 mixColor;\n
-  uniform lowp float opacity;\n
   uniform lowp float preMultipliedAlpha;\n
-  lowp vec4 visualMixColor()\n
-  {\n
-    return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
-  }\n
+  \n
   void main()\n
   {\n
-    gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * visualMixColor();\n
+    gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * vec4( mixColor, 1.0 );\n
   }\n
 );
 
@@ -159,13 +156,9 @@ const char* FRAGMENT_MASK_SHADER = DALI_COMPOSE_SHADER(
   uniform sampler2D sMask;\n
   uniform lowp vec4 uColor;\n
   uniform lowp vec3 mixColor;\n
-  uniform lowp float opacity;\n
   uniform lowp float preMultipliedAlpha;\n
   uniform mediump float auxiliaryImageAlpha;\n
-  lowp vec4 visualMixColor()\n
-  {\n
-    return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
-  }\n
+  \n
   void main()\n
   {\n
       // Where mask image is transparent, all of background image must show through.
@@ -178,7 +171,7 @@ const char* FRAGMENT_MASK_SHADER = DALI_COMPOSE_SHADER(
 
       mediump vec3 mixedColor = color.rgb * mix( 1.0-mask.a, 1.0, 1.0-auxiliaryImageAlpha)
                                 + mask.rgb*mask.a * auxiliaryImageAlpha;\n
-      gl_FragColor = vec4(mixedColor,1.0) * uColor * visualMixColor();\n
+      gl_FragColor = vec4(mixedColor,1.0) * uColor * vec4( mixColor, 1.0 );\n
   }\n
 );
 
@@ -234,13 +227,13 @@ void AddVertex( Vector< Vector2 >& vertices, unsigned int x, unsigned int y )
   vertices.PushBack( Vector2( x, y ) );
 }
 
-void RegisterStretchProperties( Renderer& renderer, const char * uniformName, const NinePatchImage::StretchRanges& stretchPixels, uint16_t imageExtent)
+void RegisterStretchProperties( Renderer& renderer, const char * uniformName, const NPatchLoader::StretchRanges& stretchPixels, uint16_t imageExtent)
 {
   uint16_t prevEnd = 0;
   uint16_t prevFix = 0;
   uint16_t prevStretch = 0;
   unsigned int i = 1;
-  for( NinePatchImage::StretchRanges::ConstIterator it = stretchPixels.Begin(); it != stretchPixels.End(); ++it, ++i )
+  for( NPatchLoader::StretchRanges::ConstIterator it = stretchPixels.Begin(); it != stretchPixels.End(); ++it, ++i )
   {
     uint16_t start = it->GetX();
     uint16_t end = it->GetY();
@@ -298,7 +291,11 @@ void NPatchVisual::LoadImages()
 {
   if( NPatchLoader::UNINITIALIZED_ID == mId && mImageUrl.IsLocalResource() )
   {
-    mId = mLoader.Load( mImageUrl.GetUrl(), mBorder );
+    bool preMultiplyOnLoad = mFactoryCache.GetPreMultiplyOnLoad() && !mImpl->mCustomShader ? true : false;
+
+    mId = mLoader.Load( mImageUrl.GetUrl(), mBorder, preMultiplyOnLoad );
+
+    EnablePreMultipliedAlpha( preMultiplyOnLoad );
   }
 
   if( ! mAuxiliaryPixelBuffer && mAuxiliaryUrl.IsValid() && mAuxiliaryUrl.IsLocalResource() )
@@ -428,7 +425,7 @@ void NPatchVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
 }
 
 NPatchVisual::NPatchVisual( VisualFactoryCache& factoryCache )
-: Visual::Base( factoryCache ),
+: Visual::Base( factoryCache, Visual::FittingMode::FILL ),
   mLoader( factoryCache.GetNPatchLoader() ),
   mImageUrl(),
   mAuxiliaryUrl(),
@@ -480,8 +477,8 @@ Shader NPatchVisual::CreateShader()
   const NPatchLoader::Data* data;
   // 0 is either no data (load failed?) or no stretch regions on image
   // for both cases we use the default shader
-  NinePatchImage::StretchRanges::SizeType xStretchCount = 0;
-  NinePatchImage::StretchRanges::SizeType yStretchCount = 0;
+  NPatchLoader::StretchRanges::SizeType xStretchCount = 0;
+  NPatchLoader::StretchRanges::SizeType yStretchCount = 0;
 
   auto fragmentShader = mAuxiliaryPixelBuffer ? FRAGMENT_MASK_SHADER
                                               : FRAGMENT_SHADER;
@@ -569,8 +566,8 @@ void NPatchVisual::ApplyTextureAndUniforms()
       Uint16Pair stretchX = data->stretchPixelsX[ 0 ];
       Uint16Pair stretchY = data->stretchPixelsY[ 0 ];
 
-      uint16_t stretchWidth = stretchX.GetY() - stretchX.GetX();
-      uint16_t stretchHeight = stretchY.GetY() - stretchY.GetX();
+      uint16_t stretchWidth = ( stretchX.GetY() >= stretchX.GetX() ) ? stretchX.GetY() - stretchX.GetX() : 0;
+      uint16_t stretchHeight = ( stretchY.GetY() >= stretchY.GetX() ) ? stretchY.GetY() - stretchY.GetX() : 0;
 
       mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
       mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2( stretchX.GetX(), stretchY.GetX()) );
@@ -591,7 +588,7 @@ void NPatchVisual::ApplyTextureAndUniforms()
     DALI_LOG_ERROR("The N patch image '%s' is not a valid N patch image\n", mImageUrl.GetUrl().c_str() );
     textureSet = TextureSet::New();
 
-    Image croppedImage = VisualFactoryCache::GetBrokenVisualImage();
+    Image croppedImage = mFactoryCache.GetBrokenVisualImage();
     TextureSetImage( textureSet, 0u, croppedImage );
     mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
     mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2::ZERO );