Added missing newline chars to logging commands
[platform/core/uifw/dali-core.git] / dali / internal / event / actors / image-actor-impl.cpp
index b6b14f8..92016de 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2016 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.
@@ -24,6 +24,7 @@
 // INTERNAL INCLUDES
 #include <dali/public-api/animation/constraints.h> // for EqualToConstraint
 #include <dali/public-api/object/type-registry.h>
+#include <dali/public-api/rendering/renderer.h>
 #include <dali/devel-api/scripting/scripting.h>
 #include <dali/internal/event/animation/constraint-impl.h>
 #include <dali/internal/event/common/property-helper.h>
@@ -59,39 +60,17 @@ TypeRegistration mType( typeid( Dali::ImageActor ), typeid( Dali::Actor ), Creat
 
 struct GridVertex
 {
+  GridVertex( float positionX, float positionY, const Vector2& size )
+  : mPosition( positionX*size.x, positionY*size.y, 0.f ),
+    mTextureCoord( positionX+0.5f, positionY+0.5f )
+  {
+  }
+
   Vector3 mPosition;
   Vector2 mTextureCoord;
 };
 
-GeometryPtr CreateQuadGeometry( const Vector2& size, unsigned int imageWidth, unsigned int imageHeight, const Dali::ImageActor::PixelArea& pixelArea )
-{
-  const float halfWidth = size.width * 0.5f;
-  const float halfHeight = size.height * 0.5f;
-  GridVertex quadVertexData[4] =
-  {
-      { Vector3( -halfWidth, -halfHeight, 0.f ), Vector2( ( pixelArea.x                   ) / (float)imageWidth, ( pixelArea.y                    ) / (float)imageHeight ) },
-      { Vector3( -halfWidth,  halfHeight, 0.f ), Vector2( ( pixelArea.x                   ) / (float)imageWidth, ( pixelArea.y + pixelArea.height ) / (float)imageHeight ) },
-      { Vector3(  halfWidth, -halfHeight, 0.f ), Vector2( ( pixelArea.x + pixelArea.width ) / (float)imageWidth, ( pixelArea.y                    ) / (float)imageHeight ) },
-      { Vector3(  halfWidth,  halfHeight, 0.f ), Vector2( ( pixelArea.x + pixelArea.width ) / (float)imageWidth, ( pixelArea.y + pixelArea.height ) / (float)imageHeight ) }
-  };
-
-  Property::Map quadVertexFormat;
-  quadVertexFormat["aPosition"] = Property::VECTOR3;
-  quadVertexFormat["aTexCoord"] = Property::VECTOR2;
-  PropertyBufferPtr quadVertices = PropertyBuffer::New();
-  quadVertices->SetFormat( quadVertexFormat );
-  quadVertices->SetSize( 4 );
-  quadVertices->SetData(quadVertexData);
-
-  // Create the geometry object
-  GeometryPtr geometry = Geometry::New();
-  geometry->AddVertexBuffer( *quadVertices );
-  geometry->SetGeometryType( Dali::Geometry::TRIANGLE_STRIP );
-
-  return geometry;
-}
-
-GeometryPtr CreateGridGeometry( const Vector2& size, unsigned int gridWidth, unsigned int gridHeight, unsigned int imageWidth, unsigned int imageHeight, const Dali::ImageActor::PixelArea& pixelArea )
+GeometryPtr CreateGeometry( unsigned int gridWidth, unsigned int gridHeight, const Vector2& size )
 {
   // Create vertices
   std::vector< GridVertex > vertices;
@@ -103,17 +82,12 @@ GeometryPtr CreateGridGeometry( const Vector2& size, unsigned int gridWidth, uns
     for( unsigned int x = 0u; x < gridWidth + 1; ++x )
     {
       float xPos = (float)x / gridWidth;
-      GridVertex vertex = {
-                            Vector3( size.width * ( xPos - 0.5f ), size.height * ( yPos - 0.5f ), 0.0f ),
-                            Vector2( ( pixelArea.x + pixelArea.width  * xPos ) / (float)imageWidth,
-                                     ( pixelArea.y + pixelArea.height * yPos ) / (float)imageHeight )
-                          };
-      vertices.push_back( vertex );
+      vertices.push_back( GridVertex( xPos - 0.5f, yPos - 0.5f, size ) );
     }
   }
 
   // Create indices
-  Vector< unsigned int > indices;
+  Vector< unsigned short > indices;
   indices.Reserve( ( gridWidth + 2 ) * gridHeight * 2 - 2);
 
   for( unsigned int row = 0u; row < gridHeight; ++row )
@@ -138,36 +112,25 @@ GeometryPtr CreateGridGeometry( const Vector2& size, unsigned int gridWidth, uns
     }
   }
 
-
   Property::Map vertexFormat;
   vertexFormat[ "aPosition" ] = Property::VECTOR3;
   vertexFormat[ "aTexCoord" ] = Property::VECTOR2;
-  PropertyBufferPtr vertexPropertyBuffer = PropertyBuffer::New();
-  vertexPropertyBuffer->SetFormat( vertexFormat );
-  vertexPropertyBuffer->SetSize( vertices.size() );
+  PropertyBufferPtr vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
   if( vertices.size() > 0 )
   {
-    vertexPropertyBuffer->SetData( &vertices[ 0 ] );
-  }
-
-  Property::Map indexFormat;
-  indexFormat[ "indices" ] = Property::INTEGER;
-  PropertyBufferPtr indexPropertyBuffer = PropertyBuffer::New();
-  indexPropertyBuffer->SetFormat( indexFormat );
-  indexPropertyBuffer->SetSize( indices.Size() );
-  if( indices.Size() > 0 )
-  {
-    indexPropertyBuffer->SetData( &indices[ 0 ] );
+    vertexPropertyBuffer->SetData( &vertices[ 0 ], vertices.size() );
   }
 
   // Create the geometry object
   GeometryPtr geometry = Geometry::New();
   geometry->AddVertexBuffer( *vertexPropertyBuffer );
-  geometry->SetIndexBuffer( *indexPropertyBuffer );
-  geometry->SetGeometryType( Dali::Geometry::TRIANGLE_STRIP );
+  if( indices.Size() > 0 )
+  {
+    geometry->SetIndexBuffer( &indices[0], indices.Size() );
+  }
+  geometry->SetType( Dali::Geometry::TRIANGLE_STRIP );
 
   return geometry;
-
 }
 
 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
@@ -176,15 +139,12 @@ const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
   varying mediump vec2 vTexCoord;\n
   uniform mediump mat4 uMvpMatrix;\n
   uniform mediump vec3 uSize;\n
-  uniform mediump vec4 uTextureRect;\n
+  uniform mediump vec4 sTextureRect;\n
   \n
   void main()\n
   {\n
-    mediump vec4 vertexPosition = vec4(aPosition, 1.0);\n
-    vertexPosition = uMvpMatrix * vertexPosition;\n
-    \n
+    gl_Position = uMvpMatrix * vec4(aPosition, 1.0);\n
     vTexCoord = aTexCoord;\n
-    gl_Position = vertexPosition;\n
   }\n
 );
 
@@ -199,9 +159,10 @@ const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
   }\n
 );
 
-const size_t INVALID_TEXTURE_ID = (size_t)-1;
+const char * const TEXTURE_RECT_UNIFORM_NAME( "sTextureRect" );
+
 const int INVALID_RENDERER_ID = -1;
-const unsigned int MAXIMUM_GRID_SIZE = 2048;
+const uint16_t MAXIMUM_GRID_SIZE = 2048;
 }
 
 ImageActorPtr ImageActor::New()
@@ -214,13 +175,13 @@ ImageActorPtr ImageActor::New()
   //Create the renderer
   actor->mRenderer = Renderer::New();
 
-  GeometryPtr quad = CreateQuadGeometry( Vector2::ONE, 1, 1, PixelArea() );
+  GeometryPtr quad  = CreateGeometry( 1u, 1u, Vector2::ONE );
   actor->mRenderer->SetGeometry( *quad );
 
-  ShaderPtr shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER, Dali::Shader::HINT_NONE );
-  MaterialPtr material = Material::New();
-  material->SetShader( *shader );
-  actor->mRenderer->SetMaterial( *material );
+  ShaderPtr shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER, Dali::Shader::Hint::NONE );
+  actor->mRenderer->SetShader( *shader );
+  TextureSetPtr textureSet = TextureSet::New();
+  actor->mRenderer->SetTextures( *textureSet );
 
   return actor;
 }
@@ -246,7 +207,9 @@ void ImageActor::SetImage( ImagePtr& image )
     SamplerPtr sampler = Sampler::New();
     sampler->SetFilterMode( mMinFilter, mMagFilter );
 
-    mTextureIndex = mRenderer->GetMaterial()->AddTexture( image, "sTexture", sampler );
+    TextureSet* textureSet( mRenderer->GetTextures() );
+    textureSet->SetImage( 0u, image.Get() );
+    textureSet->SetSampler( 0u, sampler );
 
     if( mRendererIndex == INVALID_RENDERER_ID )
     {
@@ -259,12 +222,13 @@ void ImageActor::SetImage( ImagePtr& image )
     }
 
     RelayoutRequest();
+    UpdateTexureRect();
   }
 }
 
 ImagePtr ImageActor::GetImage() const
 {
-  return mRenderer->GetMaterial()->GetTexture( mTextureIndex );
+  return mRenderer->GetTextures()->GetImage( 0u );
 }
 
 void ImageActor::SetPixelArea( const PixelArea& pixelArea )
@@ -273,6 +237,7 @@ void ImageActor::SetPixelArea( const PixelArea& pixelArea )
   mIsPixelAreaSet = true;
 
   RelayoutRequest();
+  UpdateTexureRect();
 }
 
 const ImageActor::PixelArea& ImageActor::GetPixelArea() const
@@ -301,15 +266,41 @@ void ImageActor::ClearPixelArea()
   mPixelArea = PixelArea( 0, 0, imageWidth, imageHeight );
 
   RelayoutRequest();
+  UpdateTexureRect();
+}
+
+void ImageActor::SetStyle( Dali::ImageActor::Style style )
+{
+  DALI_LOG_WARNING( "SetStyle Deprecated. Only STYLE_QUAD supported.\n" );
+  mStyle = style;
+}
+
+Dali::ImageActor::Style ImageActor::GetStyle() const
+{
+  DALI_LOG_WARNING( "GetStyle Deprecated. Only STYLE_QUAD supported.\n" );
+  return mStyle;
+}
+
+void ImageActor::SetNinePatchBorder( const Vector4& border )
+{
+  DALI_LOG_WARNING( "SetNinePatchBorder Deprecated. Only STYLE_QUAD supported.\n" );
+  mNinePatchBorder = border;
+}
+
+Vector4 ImageActor::GetNinePatchBorder() const
+{
+  DALI_LOG_WARNING( "GetNinePatchBorder Deprecated. Only STYLE_QUAD supported.\n" );
+  return mNinePatchBorder;
 }
 
 ImageActor::ImageActor()
 : Actor( Actor::BASIC ),
+  mActorSize( Vector2::ZERO ),
+  mGridSize( 1u, 1u ),
   mRendererIndex( INVALID_RENDERER_ID ),
-  mTextureIndex( INVALID_TEXTURE_ID ),
-  mEffectTextureIndex( INVALID_TEXTURE_ID ),
   mMinFilter( FilterMode::DEFAULT ),
   mMagFilter( FilterMode::DEFAULT ),
+  mStyle( Dali::ImageActor::STYLE_QUAD ),
   mIsPixelAreaSet( false )
 {
 }
@@ -347,55 +338,44 @@ Vector2 ImageActor::CalculateNaturalSize() const
   return size;
 }
 
-void ImageActor::OnRelayout( const Vector2& size, RelayoutContainer& container )
+void ImageActor::UpdateGeometry()
 {
-  unsigned int gridWidth = 1;
-  unsigned int gridHeight = 1;
+  uint16_t gridWidth = 1u;
+  uint16_t gridHeight = 1u;
+
   if( mShaderEffect )
   {
-    Vector2 gridSize = mShaderEffect->GetGridSize( size );
+    Vector2 gridSize = mShaderEffect->GetGridSize( Vector2(mPixelArea.width, mPixelArea.height) );
 
     //limit the grid size
-    gridWidth = std::min( MAXIMUM_GRID_SIZE, static_cast<unsigned int>(gridSize.width) );
-    gridHeight = std::min( MAXIMUM_GRID_SIZE, static_cast<unsigned int>(gridSize.height) );
-  }
-
-  unsigned int imageWidth = 1u;
-  unsigned int imageHeight = 1u;
-  ImagePtr image = GetImage();
-  if( image )
-  {
-    imageWidth = image->GetWidth();
-    imageHeight = image->GetHeight();
+    gridWidth = std::min( MAXIMUM_GRID_SIZE, static_cast<uint16_t>(gridSize.width) );
+    gridHeight = std::min( MAXIMUM_GRID_SIZE, static_cast<uint16_t>(gridSize.height) );
   }
 
-  GeometryPtr geometry = gridWidth <= 1 && gridHeight <= 1 ?
-                         CreateQuadGeometry( size, imageWidth, imageHeight, mPixelArea ) :
-                         CreateGridGeometry( size, gridWidth, gridHeight, imageWidth, imageHeight, mPixelArea );
+  mGridSize.SetWidth( gridWidth );
+  mGridSize.SetHeight( gridHeight );
 
+  GeometryPtr geometry = CreateGeometry( gridWidth, gridHeight, mActorSize );
   mRenderer->SetGeometry( *geometry );
-
+}
+void ImageActor::UpdateTexureRect()
+{
   Vector4 textureRect( 0.f, 0.f, 1.f, 1.f );
+
+  ImagePtr image = GetImage();
   if( mIsPixelAreaSet && image )
   {
-    const float uScale = 1.0f / float(imageWidth);
-    const float vScale = 1.0f / float(imageHeight);
-    const float x = uScale * float(mPixelArea.x);
-    const float y = vScale * float(mPixelArea.y);
-    const float width  = uScale * float(mPixelArea.width);
-    const float height = vScale * float(mPixelArea.height);
-
+    const float uScale = 1.0f / float(image->GetWidth());
+    const float vScale = 1.0f / float(image->GetHeight());
     // bottom left
-    textureRect.x = x;
-    textureRect.y = y;
-
+    textureRect.x = uScale * float(mPixelArea.x);
+    textureRect.y = vScale * float(mPixelArea.y);
     // top right
-    textureRect.z = x + width;
-    textureRect.w = y + height;
+    textureRect.z  = uScale * float(mPixelArea.x + mPixelArea.width);
+    textureRect.w = vScale * float(mPixelArea.y + mPixelArea.height);
   }
 
-  Material* material = mRenderer->GetMaterial();
-  material->RegisterProperty( "sTextureRect", textureRect );
+  mRenderer->RegisterProperty( TEXTURE_RECT_UNIFORM_NAME, textureRect );
 }
 
 unsigned int ImageActor::GetDefaultPropertyCount() const
@@ -625,62 +605,52 @@ float ImageActor::GetSortModifier() const
   return mRenderer->GetDepthIndex();
 }
 
-void ImageActor::SetCullFace(CullFaceMode mode)
-{
-  mRenderer->GetMaterial()->SetFaceCullingMode( static_cast< Dali::Material::FaceCullingMode >( mode ) );
-}
-
-CullFaceMode ImageActor::GetCullFace() const
-{
-  return static_cast< CullFaceMode >( mRenderer->GetMaterial()->GetFaceCullingMode() );
-}
-
 void ImageActor::SetBlendMode( BlendingMode::Type mode )
 {
-  mRenderer->GetMaterial()->SetBlendMode( mode );
+  mRenderer->SetBlendMode( static_cast<BlendMode::Type>( mode ) );
 }
 
 BlendingMode::Type ImageActor::GetBlendMode() const
 {
-  return mRenderer->GetMaterial()->GetBlendMode();
+  return static_cast<BlendingMode::Type>( mRenderer->GetBlendMode() );
 }
 
 void ImageActor::SetBlendFunc( BlendingFactor::Type srcFactorRgba,   BlendingFactor::Type destFactorRgba )
 {
-  mRenderer->GetMaterial()->SetBlendFunc( srcFactorRgba, destFactorRgba, srcFactorRgba, destFactorRgba );
+  mRenderer->SetBlendFunc( static_cast<BlendFactor::Type>(srcFactorRgba), static_cast<BlendFactor::Type>(destFactorRgba), static_cast<BlendFactor::Type>(srcFactorRgba), static_cast<BlendFactor::Type>(destFactorRgba) );
 }
 
 void ImageActor::SetBlendFunc( BlendingFactor::Type srcFactorRgb,   BlendingFactor::Type destFactorRgb,
                                BlendingFactor::Type srcFactorAlpha, BlendingFactor::Type destFactorAlpha )
 {
-  mRenderer->GetMaterial()->SetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
+  mRenderer->SetBlendFunc( static_cast<BlendFactor::Type>(srcFactorRgb), static_cast<BlendFactor::Type>(destFactorRgb), static_cast<BlendFactor::Type>(srcFactorAlpha), static_cast<BlendFactor::Type>(destFactorAlpha) );
 }
 
 void ImageActor::GetBlendFunc( BlendingFactor::Type& srcFactorRgb,   BlendingFactor::Type& destFactorRgb,
                                BlendingFactor::Type& srcFactorAlpha, BlendingFactor::Type& destFactorAlpha ) const
 {
-  mRenderer->GetMaterial()->GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
+  mRenderer->GetBlendFunc( reinterpret_cast<BlendFactor::Type&>(srcFactorRgb), reinterpret_cast<BlendFactor::Type&>(destFactorRgb), reinterpret_cast<BlendFactor::Type&>(srcFactorAlpha), reinterpret_cast<BlendFactor::Type&>(destFactorAlpha) );
 }
 
 void ImageActor::SetBlendEquation( BlendingEquation::Type equationRgba )
 {
-  mRenderer->GetMaterial()->SetBlendEquation( equationRgba, equationRgba );
+  mRenderer->SetBlendEquation( static_cast<BlendEquation::Type>(equationRgba), static_cast<BlendEquation::Type>(equationRgba) );
 }
 
 void ImageActor::SetBlendEquation( BlendingEquation::Type equationRgb, BlendingEquation::Type equationAlpha )
 {
-  mRenderer->GetMaterial()->SetBlendEquation( equationRgb, equationAlpha );
+  mRenderer->SetBlendEquation( static_cast<BlendEquation::Type>(equationRgb), static_cast<BlendEquation::Type>(equationAlpha) );
 }
 
 void ImageActor::GetBlendEquation( BlendingEquation::Type& equationRgb, BlendingEquation::Type& equationAlpha ) const
 {
-  mRenderer->GetMaterial()->GetBlendEquation( equationRgb, equationAlpha );
+  mRenderer->GetBlendEquation( reinterpret_cast<BlendEquation::Type&>(equationRgb), reinterpret_cast<BlendEquation::Type&>(equationAlpha) );
 }
 
 void ImageActor::SetBlendColor( const Vector4& color )
 {
   mBlendColor = color;
-  mRenderer->GetMaterial()->SetBlendColor( mBlendColor );
+  mRenderer->SetBlendColor( mBlendColor );
 }
 
 const Vector4& ImageActor::GetBlendColor() const
@@ -693,13 +663,9 @@ void ImageActor::SetFilterMode( FilterMode::Type minFilter, FilterMode::Type mag
   mMinFilter = minFilter;
   mMagFilter = magFilter;
 
-  if( mTextureIndex != INVALID_TEXTURE_ID )
-  {
-    SamplerPtr sampler = Sampler::New();
-    sampler->SetFilterMode( minFilter, magFilter );
-
-    mRenderer->GetMaterial()->SetTextureSampler( mTextureIndex, sampler.Get() );
-  }
+  SamplerPtr sampler = Sampler::New();
+  sampler->SetFilterMode( minFilter, magFilter );
+  mRenderer->GetTextures()->SetSampler( 0u, sampler.Get() );
 }
 
 void ImageActor::GetFilterMode( FilterMode::Type& minFilter, FilterMode::Type& magFilter ) const
@@ -719,9 +685,11 @@ void ImageActor::SetShaderEffect( ShaderEffect& effect )
   effect.Connect( this );
 
   ShaderPtr shader = mShaderEffect->GetShader();
-  mRenderer->GetMaterial()->SetShader( *shader );
+  mRenderer->SetShader( *shader );
 
   EffectImageUpdated();
+
+  UpdateGeometry();
 }
 
 ShaderEffectPtr ImageActor::GetShaderEffect() const
@@ -731,20 +699,16 @@ ShaderEffectPtr ImageActor::GetShaderEffect() const
 
 void ImageActor::RemoveShaderEffect()
 {
-  //if we previously had a subdivided grid then we need to reset the geometry as well
   if( mShaderEffect )
   {
     mShaderEffect->Disconnect( this );
+    // change to the standard shader and quad geometry
+    ShaderPtr shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER, Dali::Shader::Hint::NONE );
+    mRenderer->SetShader( *shader );
+    mShaderEffect.Reset();
 
-    if( mShaderEffect->GetGridSize( Vector2(INFINITY, INFINITY) ) != Vector2::ONE )
-    {
-      RelayoutRequest();
-    }
+    UpdateGeometry();
   }
-
-  ShaderPtr shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER, Dali::Shader::HINT_NONE );
-  mRenderer->GetMaterial()->SetShader( *shader );
-  mShaderEffect.Reset();
 }
 
 void ImageActor::EffectImageUpdated()
@@ -755,32 +719,31 @@ void ImageActor::EffectImageUpdated()
     if( effectImage )
     {
       Image& effectImageImpl = GetImplementation( effectImage );
-
-      if( mEffectTextureIndex == INVALID_TEXTURE_ID )
-      {
-        mEffectTextureIndex = mRenderer->GetMaterial()->AddTexture( &effectImageImpl, "sEffect", NULL );
-      }
-      else
-      {
-        mRenderer->GetMaterial()->SetTextureImage( mEffectTextureIndex, &effectImageImpl );
-      }
+      mRenderer->GetTextures()->SetImage( 1u, &effectImageImpl );
     }
     else
     {
-      if( mEffectTextureIndex != INVALID_TEXTURE_ID )
-      {
-        mRenderer->GetMaterial()->RemoveTexture( mEffectTextureIndex );
-      }
-      mEffectTextureIndex = INVALID_TEXTURE_ID;
+       mRenderer->GetTextures()->SetImage( 1u, 0 );
     }
+  }
+}
 
-    //ensure that the sEffectRect uniform is set
-    Shader* shader = mRenderer->GetMaterial()->GetShader();
-    Property::Index index = shader->GetPropertyIndex( "sEffectRect" );
-    if( index == Property::INVALID_INDEX )
-    {
-      shader->RegisterProperty( "sEffectRect", Vector4( 0.f, 0.f, 1.f, 1.f) );
-    }
+void ImageActor::OnRelayout( const Vector2& size, RelayoutContainer& container )
+{
+  if( mActorSize != size )
+  {
+    mActorSize = size;
+    UpdateGeometry();
+  }
+}
+
+void ImageActor::OnSizeSet( const Vector3& targetSize )
+{
+  Vector2 size( targetSize.x, targetSize.y );
+  if( mActorSize != size )
+  {
+    mActorSize = size;
+    UpdateGeometry();
   }
 }