Updated Gradient renderer to use SetTexture
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / renderers / control-renderer-impl.cpp
index 18374e1..4155a9c 100644 (file)
@@ -29,10 +29,10 @@ namespace
 {
 //custom shader
 const char * const CUSTOM_SHADER( "shader" );
-const char * const CUSTOM_VERTEX_SHADER( "vertex-shader" );
-const char * const CUSTOM_FRAGMENT_SHADER( "fragment-shader" );
-const char * const CUSTOM_SUBDIVIDE_GRID_X( "subdivide-grid-x" );
-const char * const CUSTOM_SUBDIVIDE_GRID_Y( "subdivide-grid-y" );
+const char * const CUSTOM_VERTEX_SHADER( "vertexShader" );
+const char * const CUSTOM_FRAGMENT_SHADER( "fragmentShader" );
+const char * const CUSTOM_SUBDIVIDE_GRID_X( "subdivideGridX" );
+const char * const CUSTOM_SUBDIVIDE_GRID_Y( "subdivideGridY" );
 const char * const CUSTOM_SHADER_HINTS( "hints" ); ///< type INTEGER; (bitfield) values from enum Shader::Hints
 }
 
@@ -56,30 +56,31 @@ ControlRenderer::~ControlRenderer()
   delete mImpl;
 }
 
-void ControlRenderer::Initialize( const Property::Map& propertyMap )
+void ControlRenderer::SetCustomShader( const Property::Map& shaderMap )
 {
   if( mImpl->mCustomShader )
   {
-    mImpl->mCustomShader->SetPropertyMap( propertyMap );
+    mImpl->mCustomShader->SetPropertyMap( shaderMap );
   }
   else
   {
-    Property::Value* customShaderValue = propertyMap.Find( CUSTOM_SHADER );
-    if( customShaderValue )
-    {
-      Property::Map customShader;
-      if( customShaderValue->Get( customShader ) )
-      {
-        mImpl->mCustomShader = new Impl::CustomShader( propertyMap );
-      }
-    }
+   mImpl->mCustomShader = new Impl::CustomShader( shaderMap );
   }
-  DoInitialize( propertyMap );
+}
 
-  if( mImpl->mIsOnStage )
+void ControlRenderer::Initialize( Actor& actor, const Property::Map& propertyMap )
+{
+  Property::Value* customShaderValue = propertyMap.Find( CUSTOM_SHADER );
+  if( customShaderValue )
   {
-    InitializeRenderer( mImpl->mRenderer );
+    Property::Map shaderMap;
+    if( customShaderValue->Get( shaderMap ) )
+    {
+      SetCustomShader( shaderMap );
+    }
   }
+
+  DoInitialize( actor, propertyMap );
 }
 
 void ControlRenderer::SetSize( const Vector2& size )
@@ -99,7 +100,6 @@ void ControlRenderer::GetNaturalSize( Vector2& naturalSize ) const
 
 void ControlRenderer::SetClipRect( const Rect<int>& clipRect )
 {
-  mImpl->mClipRect = clipRect;
 }
 
 void ControlRenderer::SetOffset( const Vector2& offset )
@@ -112,7 +112,7 @@ void ControlRenderer::SetDepthIndex( float index )
   mImpl->mDepthIndex = index;
   if( mImpl->mRenderer )
   {
-    mImpl->mRenderer.SetDepthIndex( mImpl->mDepthIndex );
+    mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
   }
 }
 
@@ -121,82 +121,46 @@ float ControlRenderer::GetDepthIndex() const
   return mImpl->mDepthIndex;
 }
 
-void ControlRenderer::SetCachedRendererKey( const std::string& cachedRendererKey )
+void ControlRenderer::SetOnStage( Actor& actor )
 {
-  if( mImpl->mCachedRendererKey == cachedRendererKey )
-  {
-    return;
-  }
-  if( !mImpl->mIsOnStage )
-  {
-    mImpl->mCachedRendererKey = cachedRendererKey;
-  }
-  else
-  {
-    //remove the cached renderer from the cache if we and the cache are the only things that hold a reference to it
-    if( mImpl->mCachedRenderer && mImpl->mCachedRenderer->ReferenceCount() == 2 )
-    {
-      mFactoryCache.RemoveRenderer( mImpl->mCachedRenderer->mKey );
-    }
-    mImpl->mCachedRenderer.Reset();
+  DoSetOnStage( actor );
 
-    //add the new renderer
-    mImpl->mCachedRendererKey = cachedRendererKey;
-    if( !mImpl->mCachedRendererKey.empty() && !mImpl->mCustomShader )
-    {
-      DALI_ASSERT_DEBUG( mImpl->mRenderer && "The control render is on stage but it doesn't have a valid renderer.");
-      mImpl->mCachedRenderer = mFactoryCache.SaveRenderer( mImpl->mCachedRendererKey, mImpl->mRenderer );
-    }
-  }
+  mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
+  mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
+  actor.AddRenderer( mImpl->mRenderer );
+  mImpl->mFlags |= Impl::IS_ON_STAGE;
 }
 
-void ControlRenderer::SetOnStage( Actor& actor )
+void ControlRenderer::SetOffStage( Actor& actor )
 {
-  if( !mImpl->mCachedRendererKey.empty() && !mImpl->mCustomShader )
+  if( GetIsOnStage() )
   {
-    mImpl->mCachedRenderer = mFactoryCache.GetRenderer( mImpl->mCachedRendererKey );
-    if( !mImpl->mCachedRenderer || !mImpl->mCachedRenderer->mRenderer )
-    {
-      InitializeRenderer( mImpl->mRenderer );
-      mImpl->mCachedRenderer = mFactoryCache.SaveRenderer( mImpl->mCachedRendererKey, mImpl->mRenderer );
-    }
+    DoSetOffStage( actor );
 
-    if( mImpl->mCachedRenderer && mImpl->mCachedRenderer->mRenderer )
-    {
-      mImpl->mRenderer = mImpl->mCachedRenderer->mRenderer;
-    }
+    mImpl->mFlags &= ~Impl::IS_ON_STAGE;
   }
+}
 
-  if( !mImpl->mRenderer )
+void ControlRenderer::EnablePreMultipliedAlpha( bool preMultipled )
+{
+  if(preMultipled)
   {
-    InitializeRenderer( mImpl->mRenderer );
+    mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
+  }
+  else
+  {
+    mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
   }
 
-  mImpl->mRenderer.SetDepthIndex( mImpl->mDepthIndex );
-  actor.AddRenderer( mImpl->mRenderer );
-  mImpl->mIsOnStage = true;
-
-  DoSetOnStage( actor );
+  if( mImpl->mRenderer )
+  {
+    mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultipled);
+  }
 }
 
-void ControlRenderer::SetOffStage( Actor& actor )
+bool ControlRenderer::IsPreMultipliedAlphaEnabled() const
 {
-  if( mImpl->mIsOnStage )
-  {
-    DoSetOffStage( actor );
-
-    //remove the cached renderer from the cache if we and the cache are the only things that hold a reference to it
-    if( mImpl->mCachedRenderer && mImpl->mCachedRenderer->ReferenceCount() == 2 )
-    {
-      mFactoryCache.RemoveRenderer( mImpl->mCachedRenderer->mKey );
-    }
-    mImpl->mCachedRenderer.Reset();
-
-    actor.RemoveRenderer( mImpl->mRenderer );
-    mImpl->mRenderer.Reset();
-
-    mImpl->mIsOnStage = false;
-  }
+  return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
 }
 
 void ControlRenderer::DoSetOnStage( Actor& actor )
@@ -205,15 +169,28 @@ void ControlRenderer::DoSetOnStage( Actor& actor )
 
 void ControlRenderer::DoSetOffStage( Actor& actor )
 {
+  actor.RemoveRenderer( mImpl->mRenderer );
+  mImpl->mRenderer.Reset();
 }
 
 void ControlRenderer::CreatePropertyMap( Property::Map& map ) const
 {
+  DoCreatePropertyMap( map );
+
   if( mImpl->mCustomShader )
   {
     mImpl->mCustomShader->CreatePropertyMap( map );
   }
-  DoCreatePropertyMap( map );
+}
+
+bool ControlRenderer::GetIsOnStage() const
+{
+  return mImpl->mFlags & Impl::IS_ON_STAGE;
+}
+
+bool ControlRenderer::GetIsFromCache() const
+{
+  return mImpl->mFlags & Impl::IS_FROM_CACHE;
 }
 
 } // namespace Internal