Split texture-manager-impl files
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / npatch / npatch-visual.h
index 91972aa..ac66c45 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_TOOLKIT_INTERNAL_N_PATCH_VISUAL_H
 
 /*
- * Copyright (c) 2016 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/common/intrusive-ptr.h>
-#include <dali/public-api/images/image.h>
 #include <dali/public-api/images/image-operations.h>
-#include <dali/devel-api/images/nine-patch-image.h>
+#include <dali/public-api/object/weak-handle.h>
 #include <dali/public-api/rendering/geometry.h>
 #include <dali/public-api/rendering/sampler.h>
 #include <dali/public-api/rendering/shader.h>
 
 // INTERNAL INCLUDES
+#include <dali-toolkit/internal/texture-manager/texture-upload-observer.h>
 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
+#include <dali-toolkit/internal/visuals/visual-url.h>
+#include <dali-toolkit/public-api/visuals/image-visual-properties.h>
 
 namespace Dali
 {
-
 namespace Toolkit
 {
-
 namespace Internal
 {
-
+class ImageVisualShaderFactory;
 class NPatchVisual;
-typedef IntrusivePtr< NPatchVisual > NPatchVisualPtr;
+typedef IntrusivePtr<NPatchVisual> NPatchVisualPtr;
 
 /**
  * The visual which renders an 9 patch image to the control's quad
@@ -50,51 +50,62 @@ typedef IntrusivePtr< NPatchVisual > NPatchVisualPtr;
  * | %Property Name           | Type             |
  * |--------------------------|------------------|
  * | url                      | STRING           |
- * | borderOnly               | BOOLEAN
- *
+ * | borderOnly               | BOOLEAN          |
+ * | border                   | RECTANGLE        |
+ * | auxiliaryImage           | STRING           |
+ * | auxiliaryImageAlpha      | FLOAT            |
  */
-class NPatchVisual: public Visual::Base
+class NPatchVisual : public Visual::Base, public TextureUploadObserver
 {
 public:
-
   /**
    * @brief Create an N-patch visual using an image URL.
    *
    * The visual will load the image synchronously when the associated actor is put on stage, and destroy the image when it is off stage
    *
    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
+   * @param[in] shaderFactory The ImageVisualShaderFactory object
    * @param[in] imageUrl The URL to 9 patch image resource to use
+   * @param[in] properties A Property::Map containing settings for this visual
+   * @return A smart-pointer to the newly allocated visual.
    */
-  static NPatchVisualPtr New( VisualFactoryCache& factoryCache, const std::string& imageUrl );
+  static NPatchVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties);
 
   /**
-   * @brief Create an N-patch visual with a NinePatchImage resource.
+   * @brief Create an N-patch visual using an image URL.
+   *
+   * The visual will load the image synchronously when the associated actor is put on stage, and destroy the image when it is off stage
    *
    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
-   * @param[in] image The NinePatchImage to use
+   * @param[in] shaderFactory The ImageVisualShaderFactory object
+   * @param[in] imageUrl The URL to 9 patch image resource to use
+   * @return A smart-pointer to the newly allocated visual.
    */
-  static NPatchVisualPtr New( VisualFactoryCache& factoryCache, NinePatchImage image );
-
-public:  // from Visual
+  static NPatchVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl);
 
+public: // from Visual
   /**
    * @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;
 
-protected:
+  /**
+   * @copydoc Visual::Base::CreateInstancePropertyMap
+   */
+  void DoCreateInstancePropertyMap(Property::Map& map) const override;
 
+protected:
   /**
    * @brief Constructor.
    *
    * @param[in] factoryCache Reference to the VisualFactoryCache object
    */
-  NPatchVisual( VisualFactoryCache& factoryCache );
+  NPatchVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory);
 
   /**
    * @brief A reference counted object may only be deleted by calling Unreference().
@@ -102,26 +113,35 @@ protected:
   virtual ~NPatchVisual();
 
   /**
+   * @copydoc Visual::Base::OnInitialize
+   */
+  void OnInitialize() override;
+
+  /**
    * @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:
+  /**
+   * Loads the NPatch image and the Auxiliary image if needed
+   */
+  void LoadImages();
 
   /**
    * @brief Creates a geometry for this renderer's grid size
@@ -147,7 +167,7 @@ private:
    * @param subType to use
    * @return the geometry
    */
-  Geometry GetNinePatchGeometry( VisualFactoryCache::GeometryType subType );
+  Geometry GetNinePatchGeometry(VisualFactoryCache::GeometryType subType);
 
   /**
    * @brief Creates a geometry for the grid size to be used by this visuals' shaders
@@ -155,7 +175,7 @@ private:
    * @param[in] gridSize The grid size of the solid geometry to create
    * @return Returns the created geometry for the grid size
    */
-  Geometry CreateGridGeometry( Uint16Pair gridSize );
+  Geometry CreateGridGeometry(Uint16Pair gridSize);
 
   /**
    * @brief Creates a geometry with the border only for the grid size to be used by this visuals' shaders
@@ -178,15 +198,34 @@ private:
    * @param[in] gridSize The grid size of the solid geometry to create
    * @return Returns the created geometry for the grid size
    */
-  Geometry CreateBorderGeometry( Uint16Pair gridSize );
+  Geometry CreateBorderGeometry(Uint16Pair gridSize);
 
-private:
+  /**
+   * @brief Creates a renderer by using loaded resource.
+   */
+  void SetResource();
 
-  NPatchLoader& mLoader;      ///< reference to N patch loader for fast access
-  std::string mImageUrl;      ///< The url to the N patch to load
-  std::size_t mId;            ///< id of the N patch (from loader/cache)
-  bool mBorderOnly;           ///< if only border is desired
+private:
+  /**
+   * @copydoc TextureUploadObserver::LoadCompleted
+   *
+   * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
+   * This callback is the place to add the renderer as it would be called once the loading is finished.
+   */
+  void LoadComplete(bool loadSuccess, TextureInformation textureInformation) override;
 
+private:
+  WeakHandle<Actor>                         mPlacementActor; ///< Weakhandle to contain Actor during texture loading
+  NPatchLoader&                             mLoader;         ///< reference to N patch loader for fast access
+  ImageVisualShaderFactory&                 mImageVisualShaderFactory;
+  VisualUrl                                 mImageUrl;             ///< The url to the N patch to load
+  VisualUrl                                 mAuxiliaryUrl;         ///< An auxiliary image that can be displayed on top of the N-Patch
+  NPatchData::NPatchDataId                  mId;                   ///< id of the N patch (from loader/cache)
+  Devel::PixelBuffer                        mAuxiliaryPixelBuffer; ///< pixel buffer of the auxiliary mask image
+  bool                                      mBorderOnly;           ///< if only border is desired
+  Rect<int>                                 mBorder;               ///< The size of the border
+  float                                     mAuxiliaryImageAlpha;  ///< The alpha value for the auxiliary image only
+  Toolkit::ImageVisual::ReleasePolicy::Type mReleasePolicy;        ///< The release policy to determine when an image should no longer be cached.
 };
 
 } // namespace Internal