#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.
*
*/
-// INTERNAL INCLUDES
-#include <dali-toolkit/internal/visuals/visual-base-impl.h>
-
// EXTERNAL INCLUDES
-#include <dali/public-api/images/image.h>
+#include <dali/public-api/common/intrusive-ptr.h>
#include <dali/public-api/images/image-operations.h>
-#include <dali/public-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;
/**
* The visual which renders an 9 patch image to the control's quad
* | %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 Constructor.
+ * @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.
*/
- NPatchVisual( VisualFactoryCache& factoryCache );
+ static NPatchVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties);
/**
- * @brief A reference counted object may only be deleted by calling Unreference().
+ * @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
+ * @return A smart-pointer to the newly allocated visual.
*/
- ~NPatchVisual();
+ static NPatchVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl);
-public: // from Visual
+public: // from Visual
+ /**
+ * @copydoc Visual::Base::GetNaturalSize
+ */
+ void GetNaturalSize(Vector2& naturalSize) override;
/**
- * @copydoc Visual::GetNaturalSize
+ * @copydoc Visual::Base::CreatePropertyMap
*/
- virtual void GetNaturalSize( Vector2& naturalSize ) const;
+ void DoCreatePropertyMap(Property::Map& map) const override;
/**
- * @copydoc Visual::CreatePropertyMap
+ * @copydoc Visual::Base::CreateInstancePropertyMap
*/
- virtual void DoCreatePropertyMap( Property::Map& map ) const;
+ void DoCreateInstancePropertyMap(Property::Map& map) const override;
protected:
+ /**
+ * @brief Constructor.
+ *
+ * @param[in] factoryCache Reference to the VisualFactoryCache object
+ */
+ NPatchVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory);
/**
- * @copydoc Visual::DoInitialize
+ * @brief A reference counted object may only be deleted by calling Unreference().
*/
- virtual void DoInitialize( Actor& actor, const Property::Map& propertyMap );
+ virtual ~NPatchVisual();
/**
- * @copydoc Visual::DoSetOnStage
+ * @copydoc Visual::Base::OnInitialize
*/
- virtual void DoSetOnStage( Actor& actor );
+ void OnInitialize() override;
/**
- * @copydoc Visual::DoSetOffStage
+ * @copydoc Visual::Base::DoSetProperties
*/
- virtual void DoSetOffStage( Actor& actor );
+ void DoSetProperties(const Property::Map& propertyMap) override;
-public:
+ /**
+ * @copydoc Visual::Base::DoSetOnScene
+ */
+ void DoSetOnScene(Actor& actor) override;
/**
- * @brief Sets the 9 patch image of this visual to the resource at imageUrl
- * 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] imageUrl The URL to 9 patch image resource to use
- * @param[in] borderOnly A Flag to indicate if the image should omit the centre of the n-patch and only render the border
+ * @copydoc Visual::Base::DoSetOffScene
*/
- void SetImage( const std::string& imageUrl, bool borderOnly = false );
+ void DoSetOffScene(Actor& actor) override;
/**
- * @brief Sets the 9 patch image of this viusal to the 9 patch image
- *
- * @param[in] image The NinePatchImage to use
- * @param[in] borderOnly A Flag to indicate if the image should omit the centre of the n-patch and only render the border
+ * @copydoc Visual::Base::OnSetTransform
*/
- void SetImage( NinePatchImage image, bool borderOnly = false );
+ void OnSetTransform() override;
private:
-
/**
- * @brief Initialize the renderer with the geometry and shader from the cache, if not available, create and save to the cache for sharing.
+ * Loads the NPatch image and the Auxiliary image if needed
*/
- void InitializeRenderer();
+ void LoadImages();
/**
* @brief Creates a geometry for this renderer's grid size
Shader CreateShader();
/**
+ * @brief Applies texture and related uniforms
+ */
+ void ApplyTextureAndUniforms();
+
+ /**
+ * Helper method to get the default Nine patch geometry from cache or create and store it there
+ * @param subType to use
+ * @return the geometry
+ */
+ Geometry GetNinePatchGeometry(VisualFactoryCache::GeometryType subType);
+
+ /**
* @brief Creates a geometry for the grid size to be used by this visuals' shaders
*
* @param[in] gridSize The grid size of the solid geometry to create
* @return Returns the created geometry for the grid size
*/
- Geometry CreateGeometry( 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
* @param[in] gridSize The grid size of the solid geometry to create
* @return Returns the created geometry for the grid size
*/
- Geometry CreateGeometryBorder( Uint16Pair gridSize );
+ Geometry CreateBorderGeometry(Uint16Pair gridSize);
/**
- * @brief Creates Image from the image url and parses the image for the stretch borders. Will create a error image if the n patch image is invalid
- *
- * @param[in] nPatchImage The NinePatchImage to base our cropped images and stretch borders from
- */
- void InitializeFromImage( NinePatchImage nPatchImage );
-
- /**
- * @brief Creates an error Image to indicate that there was an error in either the image url or the parsing of the image
- *
+ * @brief Creates a renderer by using loaded resource.
*/
- void InitializeFromBrokenImage();
-
- /**
- * @brief Applies this renderer's image to the sampler to the texture set used for this renderer
- */
- void ApplyImageToSampler();
+ void SetResource();
+private:
/**
- * @brief Changes the current renderer if the n-patch meta data has changed
+ * @copydoc TextureUploadObserver::LoadCompleted
*
- * @param[in] oldBorderOnly The old flag indicating if the image should omit the centre of the n-patch and only render the border
- * @param[in] oldGridX The old horizontal grid size of the solid geometry
- * @param[in] oldGridY The old vertical grid size of the solid geometry
+ * 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 ChangeRenderer( bool oldBorderOnly, size_t oldGridX, size_t oldGridY );
+ void LoadComplete(bool loadSuccess, TextureInformation textureInformation) override;
private:
-
- NinePatchImage mImage; ///< The image to render if the visual was set from an NinePatchImage, empty otherwise
- Image mCroppedImage;
-
- std::string mImageUrl; ///< The url to the image resource to render if the visual was set from an image resource url, empty otherwise
- NinePatchImage::StretchRanges mStretchPixelsX;
- NinePatchImage::StretchRanges mStretchPixelsY;
- ImageDimensions mImageSize;
- bool mBorderOnly;
+ 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
+ Toolkit::Visual::ResourceStatus mAuxiliaryResourceStatus; ///< resource status for 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