Fix the text visual shaders to work with pre-multiplied alpha
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-base-impl.h
index cb0c7be..c450d91 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_TOOLKIT_INTERNAL_VISUAL_H
 
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017 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.
  */
 
 // EXTERNAL INCLUDES
+#include <dali/public-api/animation/animation.h>
 #include <dali/public-api/common/intrusive-ptr.h>
 #include <dali/public-api/images/image-operations.h>
 #include <dali/public-api/object/base-object.h>
+#include <dali/public-api/rendering/renderer.h>
 #include <dali/public-api/rendering/shader.h>
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
-#include <dali-toolkit/internal/visuals/visual-factory-cache.h>
 #include <dali-toolkit/devel-api/visual-factory/visual-base.h>
+#include <dali-toolkit/internal/visuals/transition-data-impl.h>
+#include <dali-toolkit/internal/visuals/visual-factory-cache.h>
+#include <dali-toolkit/devel-api/direction-enums.h>
 
 namespace Dali
 {
@@ -41,6 +45,8 @@ namespace Internal
 namespace Visual
 {
 
+class ResourceObserver;
+
 /**
  * Base class for all Control rendering logic. A control may have multiple visuals.
  *
@@ -89,7 +95,12 @@ public:
   /**
    * @copydoc Toolkit::Visual::Base::GetHeightForWidth
    */
-  virtual float GetHeightForWidth( float width ) const;
+  virtual float GetHeightForWidth( float width );
+
+  /**
+   * @copydoc Toolkit::Visual::Base::GetWidthForHeight
+   */
+  virtual float GetWidthForHeight( float height );
 
   /**
    * @copydoc Toolkit::Visual::Base::GetNaturalSize
@@ -99,12 +110,12 @@ public:
   /**
    * @copydoc Toolkit::Visual::Base::SetDepthIndex
    */
-  void SetDepthIndex( float index );
+  void SetDepthIndex( int index );
 
   /**
    * @copydoc Toolkit::Visual::Base::GetDepthIndex
    */
-  float GetDepthIndex() const;
+  int GetDepthIndex() const;
 
   /**
    * @copydoc Toolkit::Visual::Base::SetOnStage
@@ -123,11 +134,21 @@ public:
   void CreatePropertyMap( Property::Map& map ) const;
 
   /**
+   * @brief Create a property map containing per-instance visual properties.
+   *
+   * This will enable creation of new visuals on control state change with
+   * any alternative style properties and the relevant instance properties
+   * (e.g. for image visual, the desired size, and for text visual, the actual text).
+   * @param[in] map The property map into which to write
+   */
+  void CreateInstancePropertyMap( Property::Map& map ) const;
+
+  /**
    * @brief Set whether the Pre-multiplied Alpha Blending is required
    *
-   * @param[in] preMultipled whether alpha is pre-multiplied.
+   * @param[in] preMultiplied whether alpha is pre-multiplied.
    */
-  void EnablePreMultipliedAlpha( bool preMultipled );
+  void EnablePreMultipliedAlpha( bool preMultiplied );
 
   /**
    * @brief Query whether alpha is pre-multiplied.
@@ -152,6 +173,67 @@ public:
    */
   Dali::Property::Value GetProperty( Dali::Property::Index index );
 
+  /**
+   * Gets currently staged renderer, or an empty handle if not staged
+   */
+  Renderer GetRenderer();
+
+  /**
+   * Sets the mix color ( including opacity )  of the visual.
+   * @param[in] mixColor The new mix color
+   */
+  void SetMixColor( const Vector4& color );
+
+  /**
+   * Sets the mix color of the visual.
+   * @param[in] mixColor The new mix color
+   */
+  void SetMixColor( const Vector3& color );
+
+  /**
+   * Gets the mix color of the visual.
+   * @return The mix color
+   */
+  const Vector4& GetMixColor() const;
+
+  /**
+   * Animate the property if it exists in the visual or renderer.
+   *
+   * If it's a visual property such as mix color or a transform property,
+   * saves the target value to the local data.
+   *
+   * If the visual isn't staged (i.e. it doesn't have a renderer),
+   * then this will not add an animation.
+   *
+   * @param[in] transition The animation to create or attach to
+   * @param[in] animator The animation parameters of the property.
+   */
+  void AnimateProperty( Dali::Animation& transition,
+                        Internal::TransitionData::Animator& animator );
+
+  /**
+   * @brief Add an observer to watch for when the Visuals resources are loaded.
+   * Currently only supports a single observer
+   *
+   */
+  void AddResourceObserver( Visual::ResourceObserver& observer );
+
+  /**
+   * @brief Remove an observer
+   */
+  void RemoveResourceObserver( Visual::ResourceObserver& observer );
+
+  /**
+   * @brief Called when the visuals resources are loaded / ready
+   */
+  void ResourceReady();
+
+  /**
+   * @brief Called when the visuals resources are loaded / ready
+   * @return true if ready, false otherwise
+   */
+  bool IsResourceReady() const;
+
 protected:
 
   /**
@@ -167,6 +249,7 @@ protected:
   virtual ~Base();
 
 protected:
+
   /**
    * @brief Called by CreatePropertyMap() allowing sub classes to respond to the CreatePropertyMap event
    *
@@ -175,6 +258,15 @@ protected:
   virtual void DoCreatePropertyMap( Property::Map& map ) const = 0;
 
   /**
+   * @brief Called by CreateInstancePropertyMap() allowing derived
+   * classes to store instanced data (separate to styled data) that
+   * needs copying between visuals on state change.
+   *
+   * @param[out] map The visual property map
+   */
+  virtual void DoCreateInstancePropertyMap( Property::Map& map ) const = 0;
+
+  /**
    * @brief Called by SetProperties() allowing sub classes to set their properties
    *
    * @param[in] propertyMap The properties for the requested Visual object.
@@ -187,8 +279,6 @@ protected:
    */
   virtual void OnSetTransform() = 0;
 
-protected:
-
   /**
    * @brief Called by SetOnStage() allowing sub classes to respond to the SetOnStage event
    *
@@ -214,32 +304,93 @@ protected:
    */
   bool IsOnStage() const;
 
+private:
+
+  /**
+   * Register the mix color uniform on the Renderer and store the property index.
+   * Note, this is not used by Color or Primitive Visuals, which will use their
+   * own property index.
+   */
+  void RegisterMixColor();
+
+  /**
+   * Find the matching property on the renderer or shader. If it's a shader
+   * property, register it on the renderer in order to animate it for this
+   * visual independently.
+   * @param[in] key The key to match.
+   * @return the matching index, or INVALID_INDEX if it's not found
+   */
+  Property::Index GetPropertyIndex( Property::Key key );
+
   /**
-   * @brief Gets whether the Dali::Renderer is from a shared cache (and therefore any modifications will affect other users of that renderer)
+   * Set up the transition. If no animation is required, then
+   * transition will be untouched.
    *
-   * @return Returns true if the renderer is from shared cache, false otherwise
+   * @param[in] transition The transition to use or set up.
+   * @param[in] animator The animation data to use
+   * @param[in] index The property index on the renderer to animate
+   * @param[in] initialValue The optional initial value
+   * @param[in] targetValue The target value to use
    */
-  bool IsFromCache() const;
+  void SetupTransition( Dali::Animation& transition,
+                        Internal::TransitionData::Animator& animator,
+                        Property::Index index,
+                        Property::Value& initialValue,
+                        Property::Value& targetValue );
 
-protected:
   /**
-   * @brief Called by SetProperty(). To be overriden by derived clases in order to set properties.
+   * Animate the opacity property - Special handling to
+   * ensure that the blend mode is set to ON whilst animating,
+   * and set back to AUTO if it's opaque at the end of the
+   * animation.
    *
-   * @param [in] index The index of the property.
-   * @param [in] propertyValue The new value of the property.
+   * @param[in] transition The transition to use or set up.
+   * @param[in] animator The animation data to use
    */
-  virtual void DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue ) = 0;
+  void AnimateOpacityProperty( Dali::Animation& transition,
+                               Internal::TransitionData::Animator& animator );
 
   /**
-   * @brief Called by GetProperty(). To be overriden by derived classes in order to retrieve properties.
+   * Animate the renderer property - no special handling
    *
-   * @param [in] index The index of the property.
+   * @param[in] transition The transition to use or set up.
+   * @param[in] animator The animation data to use
+   */
+  void AnimateRendererProperty( Dali::Animation& transition,
+                                Internal::TransitionData::Animator& animator );
+
+  /**
+   * Animate the mix color property.
    *
-   * @return The property value.
+   * If the animator is a vec3, then it only animates the color
+   * channels without animating the opacity.  If it's a vec4, then it
+   * runs 2 animators, one for the the vec3 mixColor, and one for the
+   * opacity. (They are separate uniforms in the shader )
+   *
+   * @param[in] transition The transition to use or set up.
+   * @param[in] animator The animation data to use
    */
-  virtual Dali::Property::Value DoGetProperty( Dali::Property::Index index ) = 0;
+  void AnimateMixColorProperty( Dali::Animation& transition,
+                                Internal::TransitionData::Animator& animator );
 
-private:
+  /**
+   * Set up the right blend mode if the opacity is being animated.
+   * Also ensure that when the animation finishes, the blend mode is
+   * set to the appropriate value. It also uses the target value as
+   * set into mMixColor.
+   *
+   * @param[in] transition The transition to listen to
+   * @param[in] isInitialOpaque Whether the initial value is opaque
+   * @param[in] animating If the transition animates the value.
+   */
+  void SetupBlendMode( Dali::Animation& transition,
+                       bool isInitialOpaque, bool animating );
+
+  /**
+   * When a mix color animation has finished, ensure the blend mode is set back
+   * to the right value for the target opacity.
+   */
+  void OnMixColorFinished( Animation& animation );
 
   // Undefined
   Base( const Visual::Base& visual );