ImageVisual Load policy
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / image / image-visual.h
index 56b1ae1..87111b7 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_TOOLKIT_INTERNAL_IMAGE_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.
  *
  */
 
-// INTERNAL INCLUDES
-#include <dali-toolkit/internal/visuals/visual-base-impl.h>
-#include <dali-toolkit/devel-api/image-loader/atlas-upload-observer.h>
-
 // EXTERNAL INCLUDES
+#include <memory>
+
+#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/public-api/images/resource-image.h>
-#include <dali/devel-api/object/weak-handle.h>
+#include <dali/public-api/object/weak-handle.h>
+
+// INTERNAL INCLUDES
+#include <dali-toolkit/devel-api/image-loader/atlas-upload-observer.h>
+#include <dali-toolkit/internal/visuals/texture-upload-observer.h>
+#include <dali-toolkit/internal/visuals/visual-base-impl.h>
+#include <dali-toolkit/internal/visuals/visual-url.h>
+#include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
 
 namespace Dali
 {
@@ -43,18 +49,36 @@ class ImageVisual;
 typedef IntrusivePtr< ImageVisual > ImageVisualPtr;
 
 /**
- * The visual which renders an image to the control's quad
+ * The visual which renders an image to a quad geometry
  *
  * The following properties are optional
  *
- * | %Property Name     | Type             |
- * |--------------------|------------------|
- * | url                | STRING           |
- * | fittingMode        | STRING           |
- * | samplingMode       | STRING           |
- * | desiredWidth       | INT              |
- * | desiredHeight      | INT              |
- * | synchronousLoading | BOOLEAN          |
+ * | %Property Name     | Type              |
+ * |--------------------|-------------------|
+ * | url                | STRING            |
+ * | alphaMaskUrl       | STRING            |
+ * | fittingMode        | INTEGER OR STRING |
+ * | samplingMode       | INTEGER OR STRING |
+ * | desiredWidth       | INTEGER           |
+ * | desiredHeight      | INTEGER           |
+ * | synchronousLoading | BOOLEAN           |
+ * | pixelArea          | VECTOR4           |
+ * | wrapModeU          | INTEGER OR STRING |
+ * | wrapModeV          | INTEGER OR STRING |
+ * | loadPolicy         | INTEGER OR STRING |
+ * | releasePolicy      | INTEGER OR STRING |
+ *
+ * where pixelArea is a rectangular area.
+ * In its Vector4 value, the first two elements indicate the top-left position of the area,
+ * and the last two elements are the area width and height respectively.
+ * If not specified, the default value is [0.0, 0.0, 1.0, 1.0], i.e. the entire area of the image.
+ *
+ * where wrapModeU and wrapModeV separately decide how the texture should be sampled when the u and v coordinate exceeds the range of 0.0 to 1.0.
+ * Its value should be one of the following wrap mode:
+ *   "DEFAULT"
+ *   "CLAMP_TO_EDGE"
+ *   "REPEAT"
+ *   "MIRRORED_REPEAT"
  *
  * where imageFittingMode should be one of the following fitting modes:
  *   "SHRINK_TO_FIT"
@@ -73,20 +97,44 @@ typedef IntrusivePtr< ImageVisual > ImageVisualPtr;
  *   "DONT_CARE"
  *   "DEFAULT"
  *
+ * where loadPolicy should be one of the following image loading modes
+ *   "IMMEDIATE"   // Loads image even if visual not attached to stage yet
+ *   "ATTACHED"    // Only loads image once visual is attached to stage
+ *
+ * where releasePolicy should be one of the following policies for when to cache the image
+ *   "DETACHED"    //  Release image from cache when visual detached from stage
+ *   "DESTROYED"   //  Keep image in cache until the visual is destroyed
+ *   "NEVER"       //  Keep image in cache until application ends.
+ *
+ * If the Visual is in a LayerUI it will pixel align the image, using a Layer3D will disable pixel alignment.
+ * Changing layer behaviour between LayerUI to Layer3D whilst the visual is already staged will not have an effect.
  */
-class ImageVisual: public Visual::Base, public ConnectionTracker, public AtlasUploadObserver
+class ImageVisual: public Visual::Base, public ConnectionTracker, public AtlasUploadObserver, public TextureUploadObserver
 {
 public:
 
   /**
-   * @brief Constructor.
+   * @brief Create a new image visual with a URL.
+   *
+   * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
    *
    * @param[in] factoryCache The VisualFactoryCache object
+   * @param[in] imageUrl The URL of the image resource to use
+   * @param[in] properties A Property::Map containing settings for this visual
+   * @param[in] size The width and height to fit the loaded image to.
+   * @param[in] fittingMode The FittingMode of the resource to load
+   * @param[in] samplingMode The SamplingMode of the resource to load
+   * @return A smart-pointer to the newly allocated visual.
    */
-  ImageVisual( VisualFactoryCache& factoryCache );
+  static ImageVisualPtr New( VisualFactoryCache& factoryCache,
+                             const VisualUrl& imageUrl,
+                             const Property::Map& properties,
+                             ImageDimensions size = ImageDimensions(),
+                             FittingMode::Type fittingMode = FittingMode::DEFAULT,
+                             Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
 
   /**
-   * @brief Constructor with a URL.
+   * @brief Create a new image visual with a URL.
    *
    * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
    *
@@ -95,32 +143,28 @@ public:
    * @param[in] size The width and height to fit the loaded image to.
    * @param[in] fittingMode The FittingMode of the resource to load
    * @param[in] samplingMode The SamplingMode of the resource to load
+   * @return A smart-pointer to the newly allocated visual.
    */
-  ImageVisual( VisualFactoryCache& factoryCache,
-               const std::string& imageUrl,
-               ImageDimensions size=ImageDimensions(),
-               FittingMode::Type fittingMode = FittingMode::DEFAULT,
-               Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
+  static ImageVisualPtr New( VisualFactoryCache& factoryCache,
+                             const VisualUrl& imageUrl,
+                             ImageDimensions size = ImageDimensions(),
+                             FittingMode::Type fittingMode = FittingMode::DEFAULT,
+                             Dali::SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR );
 
   /**
-   * @brief Constructor with an Image type.
+   * @brief Create a new image visual with an Image type.
    *
    * @param[in] factoryCache The VisualFactoryCache object
    * @param[in] image The image to use
    */
-  ImageVisual( VisualFactoryCache& factoryCache, const Image& image );
-
-  /**
-   * @brief A reference counted object may only be deleted by calling Unreference().
-   */
-  ~ImageVisual();
+  static ImageVisualPtr New( VisualFactoryCache& factoryCache, const Image& image );
 
 public:  // from Visual
 
   /**
    * @copydoc Visual::Base::GetNaturalSize
    */
-  virtual void GetNaturalSize( Vector2& naturalSize ) const;
+  virtual void GetNaturalSize( Vector2& naturalSize );
 
   /**
    * @copydoc Visual::Base::CreatePropertyMap
@@ -128,20 +172,46 @@ public:  // from Visual
   virtual void DoCreatePropertyMap( Property::Map& map ) const;
 
   /**
-   * @copydoc Visual::Base::DoSetProperty
+   * @copydoc Visual::Base::CreateInstancePropertyMap
    */
-  virtual void DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue );
+  virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
+
+protected:
 
   /**
-   * @copydoc Visual::Base::DoGetProperty
+   * @brief Constructor with a URL.
+   *
+   * The visual will load the Image asynchronously when the associated actor is put on stage, and destroy the image when it is off stage
+   *
+   * @param[in] factoryCache The VisualFactoryCache object
+   * @param[in] imageUrl The URL of the image resource to use
+   * @param[in] size The width and height to fit the loaded image to.
+   * @param[in] fittingMode The FittingMode of the resource to load
+   * @param[in] samplingMode The SamplingMode of the resource to load
    */
-  virtual Dali::Property::Value DoGetProperty( Dali::Property::Index index );
+  ImageVisual( VisualFactoryCache& factoryCache,
+               const VisualUrl& imageUrl,
+               ImageDimensions size,
+               FittingMode::Type fittingMode,
+               Dali::SamplingMode::Type samplingMode );
+
+  /**
+   * @brief Constructor with an Image type.
+   *
+   * @param[in] factoryCache The VisualFactoryCache object
+   * @param[in] image The image to use
+   */
+  ImageVisual( VisualFactoryCache& factoryCache, const Image& image );
 
-protected:
   /**
-   * @copydoc Visual::Base::DoInitialize
+   * @brief A reference counted object may only be deleted by calling Unreference().
    */
-  virtual void DoInitialize( Actor& actor, const Property::Map& propertyMap );
+  virtual ~ImageVisual();
+
+  /**
+   * @copydoc Visual::Base::DoSetProperties
+   */
+  virtual void DoSetProperties( const Property::Map& propertyMap );
 
   /**
    * @copydoc Visual::Base::DoSetOnStage
@@ -153,6 +223,11 @@ protected:
    */
   virtual void DoSetOffStage( Actor& actor );
 
+  /**
+   * @copydoc Visual::Base::OnSetTransform
+   */
+  virtual void OnSetTransform();
+
 public:
 
   /**
@@ -171,9 +246,22 @@ public:
    */
   virtual void UploadCompleted();
 
+  /**
+   * @copydoc TextureUploadObserver::UploadCompleted
+   *
+   * 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.
+   */
+  virtual void UploadComplete( bool success, int32_t textureId, TextureSet textureSet, bool usingAtlas, const Vector4& atlasRectangle );
+
 private:
 
   /**
+   * Allocate the mask data when a masking property is defined in the property map
+   */
+  void AllocateMaskData();
+
+  /**
    * @brief Applies the image to the texture set used for this renderer
    *
    * @param[in] image The Image to apply to the texture set used for this renderer
@@ -181,11 +269,17 @@ private:
   void ApplyImageToSampler( const Image& image );
 
   /**
-   * @brief Initializes the Dali::Renderer from an image url string
-   *
-   * @param[in] imageUrl The image url string to intialize this ImageVisual from
+   * @brief Load the texture, will try to atlas unless unable or param set to false.
+   * @param[in, out] atlasing flag if the image has been put in a atlas (true), passing false will not atlas even if possible.
+   * @param[out] atlasRect if atlasing is used this the texture area of the image in the atlas.
+   * @param[out] textures resulting texture set from the image loading.
+   */
+  void LoadTexture( bool& atlasing, Vector4& atlasRect, TextureSet& textures );
+
+  /**
+   * @brief Initializes the Dali::Renderer from the image url
    */
-  void InitializeRenderer( const std::string& imageUrl );
+  void InitializeRenderer();
 
   /**
    * @brief Initializes the Dali::Renderer from an image handle
@@ -196,17 +290,15 @@ private:
 
   /**
    * @brief Creates the Dali::Renderer (potentially from the renderer cache), initializing it
-   *
-   * @return Returns the created Dali::Renderer
+   * @param[in] textures to use
    */
-  Renderer CreateRenderer() const;
+  void CreateRenderer( TextureSet& textures );
 
   /**
    * @brief Creates the Dali::Renderer for NativeImage with custom sampler type and prefix, initializing it
-   *
-   * @return Returns the created Dali::Renderer
+   * @param NativeImageRenderer
    */
-  Renderer CreateNativeImageRenderer() const;
+  void CreateNativeImageRenderer( NativeImage& nativeImage );
 
   /**
    * @brief Query whether resources requires to be loaded synchronously.
@@ -215,30 +307,14 @@ private:
   bool IsSynchronousResourceLoading() const;
 
   /**
-   * @brief Load the resource synchronously
-   */
-  void LoadResourceSynchronously();
-
-  /**
-   * Load the image.
+   * Creates the texture set and adds the texture to it
+   * @param[out] textureRect The texture area of the texture in the atlas.
    * @param[in] url The URL of the image resource to use.
    * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
+   * @param[in] attemptAtlasing If true will attempt atlasing, otherwise create unique texture
+   * @return the texture set to use
    */
-  Image LoadImage( const std::string& url, bool synchronousLoading );
-
-  /**
-   * Load the image and create a texture set to hold the texture, with automatic atlasing applied.
-   * @param [out] textureRect The texture area of the resource image in the atlas.
-   * @param[in] url The URL of the image resource to use.
-   * @param[in] synchronousLoading If true, the resource is loaded synchronously, otherwise asynchronously.
-   */
-  TextureSet CreateTextureSet( Vector4& textureRect, const std::string& url, bool synchronousLoading );
-
-  /**
-   * Callback function of image resource loading succeed
-   * @param[in] image The Image content that we attempted to load from mImageUrl
-   */
-  void OnImageLoaded( ResourceImage image );
+  TextureSet CreateTextureSet( Vector4& textureRect, bool synchronousLoading, bool attemptAtlasing );
 
   /**
    * Set the value to the uTextureRect uniform
@@ -247,32 +323,42 @@ private:
   void SetTextureRectUniform( const Vector4& textureRect  );
 
   /**
-   * Clean the renderer from cache, and remove the image from atlas if it is not used anymore
+   * Remove texture with valid TextureId
    */
-  void CleanCache(const std::string& url);
+  void RemoveTexture();
 
   /**
-   * Set shader code for nativeimage if it exists
+   * Helper method to set individual values by index key.
+   * @param[in] index The index key of the value
+   * @param[in] value The value
    */
-  void SetNativeFragmentShaderCode( Dali::NativeImage& nativeImage );
+  void DoSetProperty( Property::Index index, const Property::Value& value );
 
 private:
+
   Image mImage;
-  PixelData mPixels;
   Vector4 mPixelArea;
   WeakHandle<Actor> mPlacementActor;
+  VisualUrl mImageUrl;
+  TextureManager::MaskingDataPointer mMaskingData;
 
-  std::string mImageUrl;
   Dali::ImageDimensions mDesiredSize;
-  Dali::FittingMode::Type mFittingMode;
-  Dali::SamplingMode::Type mSamplingMode;
-  Dali::WrapMode::Type mWrapModeU;
-  Dali::WrapMode::Type mWrapModeV;
-
-  std::string mNativeFragmentShaderCode;
-  bool mNativeImageFlag;
+  TextureManager::TextureId mTextureId;
+  TextureSet mTextures;
+
+  Dali::FittingMode::Type mFittingMode:3;
+  Dali::SamplingMode::Type mSamplingMode:4;
+  Dali::WrapMode::Type mWrapModeU:3;
+  Dali::WrapMode::Type mWrapModeV:3;
+  DevelImageVisual::LoadPolicy::Type mLoadPolicy;
+  DevelImageVisual::ReleasePolicy::Type mReleasePolicy;
+  Vector4 mAtlasRect;
+  bool mAttemptAtlasing; ///< If true will attempt atlasing, otherwise create unique texture
+  bool mLoading;  ///< True if the texture is still loading.
 };
 
+
+
 } // namespace Internal
 
 } // namespace Toolkit