Combine textvisual shader by TextShaderFactory
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / text / text-visual.h
index ef5890e..05d1585 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_TOOLKIT_INTERNAL_TEXT_VISUAL_H
 
 /*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 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/constraint.h>
 #include <dali/public-api/object/base-object.h>
 #include <dali/public-api/object/weak-handle.h>
+#include <dali/public-api/rendering/visual-renderer.h>
 
 // INTERNAL INCLUDES
-#include <dali-toolkit/internal/visuals/visual-base-impl.h>
 #include <dali-toolkit/internal/text/rendering/text-typesetter.h>
 #include <dali-toolkit/internal/text/text-controller.h>
+#include <dali-toolkit/internal/visuals/text-visual-shader-factory.h>
+#include <dali-toolkit/internal/visuals/visual-base-impl.h>
 
 namespace Dali
 {
-
 namespace Toolkit
 {
-
 namespace Internal
 {
-
 class TextVisual;
-typedef IntrusivePtr< TextVisual > TextVisualPtr;
+typedef IntrusivePtr<TextVisual> TextVisualPtr;
 
 /**
  * The visual which renders text
@@ -69,30 +69,31 @@ typedef IntrusivePtr< TextVisual > TextVisualPtr;
 class TextVisual : public Visual::Base
 {
 public:
-
   /**
    * @brief Create a new text visual.
    *
    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
+   * @param[in] shaderFactory The TextVisualShaderFactory object
    * @param[in] properties A Property::Map containing settings for this visual
    * @return A smart-pointer to the newly allocated visual.
    */
-  static TextVisualPtr New( VisualFactoryCache& factoryCache, const Property::Map& properties );
+  static TextVisualPtr New(VisualFactoryCache& factoryCache, TextVisualShaderFactory& shaderFactory, const Property::Map& properties);
 
   /**
    * @brief Converts all strings keys in property map to index keys.  Property Map can then be merged correctly.
-   * @param[in,out] propertyMap containing string keys or a mix of strings and indexes. Will be changed to index keys.
+   * @param[in] propertyMap containing string keys or a mix of strings and indexes.
+   * @return Property::Map containing index keys.
    */
-  static void ConvertStringKeysToIndexKeys( Property::Map& propertyMap );
+  static Property::Map ConvertStringKeysToIndexKeys(const Property::Map& propertyMap);
 
   /**
    * @brief Retrieve the text's controller.
    * @param[in] visual The text visual.
    * @return The text controller
    */
-  static Text::ControllerPtr GetController( Toolkit::Visual::Base visual )
+  static Text::ControllerPtr GetController(Toolkit::Visual::Base visual)
   {
-    return GetVisualObject( visual ).mController;
+    return GetVisualObject(visual).mController;
   };
 
   /**
@@ -100,79 +101,143 @@ public:
    * @param[in] visual The text visual.
    * @param[in] animatablePropertyIndex The index of the animatable property
    */
-  static void SetAnimatableTextColorProperty( Toolkit::Visual::Base visual, Property::Index animatablePropertyIndex )
+  static void SetAnimatableTextColorProperty(Toolkit::Visual::Base visual, Property::Index animatablePropertyIndex)
   {
-    GetVisualObject( visual ).mAnimatableTextColorPropertyIndex = animatablePropertyIndex;
+    GetVisualObject(visual).mAnimatableTextColorPropertyIndex = animatablePropertyIndex;
   };
 
   /**
    * @brief Set the flag to trigger the textures to be initialized and renderer to be added to the control.
    * @param[in] visual The text visual.
    */
-  static void EnableRendererUpdate( Toolkit::Visual::Base visual )
+  static void EnableRendererUpdate(Toolkit::Visual::Base visual)
   {
-    GetVisualObject( visual ).mRendererUpdateNeeded = true;
+    GetVisualObject(visual).mRendererUpdateNeeded = true;
   };
 
-public: // from Visual::Base
+  /**
+   * @brief Instantly updates the renderer
+   * @param[in] visual The text visual.
+   */
+  static void UpdateRenderer(Toolkit::Visual::Base visual)
+  {
+    GetVisualObject(visual).UpdateRenderer();
+  };
 
+public: // from Visual::Base
   /**
    * @copydoc Visual::Base::GetHeightForWidth()
    */
-  virtual float GetHeightForWidth( float width );
+  float GetHeightForWidth(float width) override;
 
   /**
    * @copydoc Visual::Base::GetNaturalSize()
    */
-  virtual void GetNaturalSize( Vector2& naturalSize );
+  void GetNaturalSize(Vector2& naturalSize) override;
 
   /**
    * @copydoc Visual::Base::CreatePropertyMap()
    */
-  virtual void DoCreatePropertyMap( Property::Map& map ) const;
+  void DoCreatePropertyMap(Property::Map& map) const override;
 
   /**
    * @copydoc Visual::Base::CreateInstancePropertyMap
    */
-  virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
+  void DoCreateInstancePropertyMap(Property::Map& map) const override;
 
-protected:
+  /**
+   * @copydoc Visual::Base::EnablePreMultipliedAlpha
+   */
+  void EnablePreMultipliedAlpha(bool preMultiplied) override;
 
+protected:
   /**
    * @brief Constructor.
    *
    * @param[in] factoryCache The VisualFactoryCache object
+   * @param[in] shaderFactory The TextVisualShaderFactory object
    */
-  TextVisual( VisualFactoryCache& factoryCache );
+  TextVisual(VisualFactoryCache& factoryCache, TextVisualShaderFactory& shaderFactory);
 
   /**
    * @brief A reference counted object may only be deleted by calling Unreference().
    */
   virtual ~TextVisual();
 
+  /**
+   * @copydoc Visual::Base::OnInitialize
+   */
+  void OnInitialize() override;
+
   // from Visual::Base
 
   /**
    * @copydoc Visual::Base::DoSetProperties()
    */
-  virtual void DoSetProperties( const Property::Map& propertyMap );
+  void DoSetProperties(const Property::Map& propertyMap) override;
 
   /**
-   * @copydoc Visual::Base::DoSetOnStage()
+   * @copydoc Visual::Base::DoSetOnScene()
    */
-  virtual void DoSetOnStage( Actor& actor );
+  void DoSetOnScene(Actor& actor) override;
 
   /**
-   * @copydoc Visual::Base::DoSetOffStage()
+   * @copydoc Visual::Base::DoSetOffScene()
    */
-  virtual void DoSetOffStage( Actor& actor );
+  void DoSetOffScene(Actor& actor) override;
 
   /**
    * @copydoc Visual::Base::OnSetTransform
    */
-  virtual void OnSetTransform();
+  void OnSetTransform() override;
 
 private:
+  struct TilingInfo
+  {
+    unsigned char* textBuffer;
+    unsigned char* styleBuffer;
+    unsigned char* overlayStyleBuffer;
+    unsigned char* maskBuffer;
+    int            width;
+    int            height;
+    Pixel::Format  textPixelFormat;
+    int            offsetPosition;
+    Vector2        offSet;
+
+    TilingInfo(int width, int height, Pixel::Format textPixelFormat)
+    : textBuffer(NULL),
+      styleBuffer(NULL),
+      overlayStyleBuffer(NULL),
+      maskBuffer(NULL),
+      width(width),
+      height(height),
+      textPixelFormat(textPixelFormat),
+      offsetPosition(0),
+      offSet(0.f, 0.f)
+    {
+    }
+
+    ~TilingInfo()
+    {
+      if(textBuffer)
+      {
+        free(textBuffer);
+      }
+      if(styleBuffer)
+      {
+        free(styleBuffer);
+      }
+      if(overlayStyleBuffer)
+      {
+        free(overlayStyleBuffer);
+      }
+      if(maskBuffer)
+      {
+        free(maskBuffer);
+      }
+    }
+  };
+
   /**
    * @brief Set the individual property to the given value.
    *
@@ -180,7 +245,7 @@ private:
    *
    * @param[in] propertyValue The value to set.
    */
-  void DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue );
+  void DoSetProperty(Dali::Property::Index index, const Dali::Property::Value& propertyValue);
 
   /**
    * @brief Updates the text's renderer.
@@ -188,62 +253,88 @@ private:
   void UpdateRenderer();
 
   /**
-   * @brief Removes the texture set from the renderer.
+   * @brief Removes the text's renderer.
+   */
+  void RemoveRenderer(Actor& actor);
+
+  /**
+   * @brief Create a texture in textureSet and add it.
+   * @param[in] textureSet The textureSet to which the texture will be added.
+   * @param[in] data The PixelData to be uploaded to texture
+   * @param[in] sampler The sampler.
+   * @param[in] textureSetIndex The Index of TextureSet.
+   */
+  void AddTexture(TextureSet& textureSet, PixelData& data, Sampler& sampler, unsigned int textureSetIndex);
+
+  /**
+   * @brief Convert the buffer to pixelData.
+   * @param[in] buffer The Buffer to be converted to pixelData.
+   * @param[in] width The width of pixel data.
+   * @param[in] height The height of pixel data.
+   * @param[in] offsetPosition The The buffer's start position.
+   * @param[in] textPixelFormat The PixelForma of text.
    */
-  void RemoveTextureSet();
+  PixelData ConvertToPixelData(unsigned char* buffer, int width, int height, int offsetPosition, const Pixel::Format textPixelFormat);
 
   /**
-   * Get the texture of the text for rendering.
+   * @brief Create the text's texture. It will use cached shader feature for text visual.
+   * @param[in] info This is the information you need to create a Tiling.
+   * @param[in] renderer The renderer to which the TextureSet will be added.
+   * @param[in] sampler The sampler.
+   */
+  void CreateTextureSet(TilingInfo& info, VisualRenderer& renderer, Sampler& sampler);
+
+  /**
+   * Create renderer of the text for rendering.
+   * @param[in] actor The actor.
    * @param[in] size The texture size.
    * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
-   * @param[in] containsEmoji Whether the text contains emoji.
+   * @param[in] containsColorGlyph Whether the text contains color glyph.
    * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
+   * @param[in] isOverlayStyle Whether the style needs to overlay on the text (e.g. strikethrough, underline, etc.).
    */
-  TextureSet GetTextTexture( const Vector2& size, bool hasMultipleTextColors, bool containsEmoji, bool styleEnabled );
+  void AddRenderer(Actor& actor, const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled, bool isOverlayStyle);
+
+  /**
+   * Get the texture of the text for rendering. It will use cached shader feature for text visual.
+   * @param[in] size The texture size.
+   */
+  TextureSet GetTextTexture(const Vector2& size);
 
   /**
    * Get the text rendering shader.
    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
-   * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
-   * @param[in] containsEmoji Whether the text contains emoji.
-   * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
+   * @param[in] featureBuilder Collection of current text shader's features. It will be cached as text visual.
    */
-  Shader GetTextShader( VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsEmoji, bool styleEnabled );
+  Shader GetTextShader(VisualFactoryCache& factoryCache, const TextVisualShaderFeature::FeatureBuilder& featureBuilder);
 
   /**
-   * @brief Retrieve the text's controller.
-   * @param[in] visual The text visual.
-   * @return The text controller
+   * @brief Retrieve the TextVisual object.
+   * @param[in] visual A handle to the TextVisual
+   * @return The TextVisual object
    */
-  static TextVisual& GetVisualObject( Toolkit::Visual::Base visual )
+  static TextVisual& GetVisualObject(Toolkit::Visual::Base visual)
   {
-    return static_cast<TextVisual&>( visual.GetBaseObject() );
+    return static_cast<TextVisual&>(Toolkit::GetImplementation(visual).GetVisualObject());
   };
 
 private:
-
-  /**
-   * Used as an alternative to boolean so that it is obvious whether the text contains single or multiple text colors, and emoji and styles.
-   */
-  struct TextType
-  {
-    enum Type
-    {
-      SINGLE_COLOR_TEXT = 0, ///< The text contains single color only.
-      MULTI_COLOR_TEXT = 1,  ///< The text contains multiple colors.
-      NO_EMOJI = 0,          ///< The text contains no emoji.
-      HAS_EMOJI = 1,         ///< The text contains emoji.
-      NO_STYLES = 0,         ///< The text contains contains no styles.
-      HAS_SYLES = 1          ///< The text contains contains styles.
-    };
-  };
+  typedef std::vector<Renderer> RendererContainer;
 
 private:
-  Text::ControllerPtr mController;                        ///< The text's controller.
-  Text::TypesetterPtr mTypesetter;                        ///< The text's typesetter.
-  WeakHandle<Actor>   mControl;                           ///< The control where the renderer is added.
-  Property::Index     mAnimatableTextColorPropertyIndex;  ///< The index of animatable text color property registered by the control.
-  bool                mRendererUpdateNeeded:1;            ///< The flag to indicate whether the renderer needs to be updated.
+  Text::ControllerPtr mController; ///< The text's controller.
+  Text::TypesetterPtr mTypesetter; ///< The text's typesetter.
+
+  TextVisualShaderFactory&                mTextVisualShaderFactory; ///< The shader factory for text visual.
+  TextVisualShaderFeature::FeatureBuilder mTextShaderFeatureCache;  ///< The cached shader feature for text visual.
+
+  WeakHandle<Actor> mControl;                          ///< The control where the renderer is added.
+  Constraint        mColorConstraint{};                ///< Color constraint
+  Constraint        mOpacityConstraint{};              ///< Opacity constraint
+  Property::Index   mAnimatableTextColorPropertyIndex; ///< The index of animatable text color property registered by the control.
+  Property::Index   mTextColorAnimatableIndex;         ///< The index of uTextColorAnimatable property.
+  bool              mRendererUpdateNeeded : 1;         ///< The flag to indicate whether the renderer needs to be updated.
+  RendererContainer mRendererList;
 };
 
 } // namespace Internal