1 #ifndef DALI_TOOLKIT_INTERNAL_N_PATCH_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_N_PATCH_VISUAL_H
5 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/common/intrusive-ptr.h>
23 #include <dali/public-api/images/image-operations.h>
24 #include <dali/public-api/object/weak-handle.h>
25 #include <dali/public-api/rendering/geometry.h>
26 #include <dali/public-api/rendering/sampler.h>
27 #include <dali/public-api/rendering/shader.h>
30 #include <dali-toolkit/internal/texture-manager/texture-upload-observer.h>
31 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
32 #include <dali-toolkit/internal/visuals/visual-url.h>
33 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
41 class ImageVisualShaderFactory;
43 typedef IntrusivePtr<NPatchVisual> NPatchVisualPtr;
46 * The visual which renders an 9 patch image to the control's quad
48 * The following properties are optional
50 * | %Property Name | Type |
51 * |--------------------------|------------------|
53 * | borderOnly | BOOLEAN |
54 * | border | RECTANGLE |
55 * | auxiliaryImage | STRING |
56 * | auxiliaryImageAlpha | FLOAT |
58 class NPatchVisual : public Visual::Base, public TextureUploadObserver
62 * @brief Create an N-patch visual using an image URL.
64 * The visual will load the image synchronously when the associated actor is put on stage, and destroy the image when it is off stage
66 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
67 * @param[in] shaderFactory The ImageVisualShaderFactory object
68 * @param[in] imageUrl The URL to 9 patch image resource to use
69 * @param[in] properties A Property::Map containing settings for this visual
70 * @return A smart-pointer to the newly allocated visual.
72 static NPatchVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties);
75 * @brief Create an N-patch visual using an image URL.
77 * The visual will load the image synchronously when the associated actor is put on stage, and destroy the image when it is off stage
79 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
80 * @param[in] shaderFactory The ImageVisualShaderFactory object
81 * @param[in] imageUrl The URL to 9 patch image resource to use
82 * @return A smart-pointer to the newly allocated visual.
84 static NPatchVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl);
86 public: // from Visual
88 * @copydoc Visual::Base::GetNaturalSize
90 void GetNaturalSize(Vector2& naturalSize) override;
93 * @copydoc Visual::Base::CreatePropertyMap
95 void DoCreatePropertyMap(Property::Map& map) const override;
98 * @copydoc Visual::Base::CreateInstancePropertyMap
100 void DoCreateInstancePropertyMap(Property::Map& map) const override;
104 * @brief Constructor.
106 * @param[in] factoryCache Reference to the VisualFactoryCache object
108 NPatchVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory);
111 * @brief A reference counted object may only be deleted by calling Unreference().
113 virtual ~NPatchVisual();
116 * @copydoc Visual::Base::OnInitialize
118 void OnInitialize() override;
121 * @copydoc Visual::Base::DoSetProperties
123 void DoSetProperties(const Property::Map& propertyMap) override;
126 * @copydoc Visual::Base::DoSetOnScene
128 void DoSetOnScene(Actor& actor) override;
131 * @copydoc Visual::Base::DoSetOffScene
133 void DoSetOffScene(Actor& actor) override;
136 * @copydoc Visual::Base::OnSetTransform
138 void OnSetTransform() override;
142 * Loads the NPatch image and the Auxiliary image if needed
147 * @brief Creates a geometry for this renderer's grid size
149 * @return Returns the created geometry for this renderer's grid size
151 Geometry CreateGeometry();
154 * @brief Creates a shader for this renderer's grid size
156 * @return Returns the created shader for this renderer's grid size
158 Shader CreateShader();
161 * @brief Applies texture and related uniforms
163 void ApplyTextureAndUniforms();
166 * Helper method to get the default Nine patch geometry from cache or create and store it there
167 * @param subType to use
168 * @return the geometry
170 Geometry GetNinePatchGeometry(VisualFactoryCache::GeometryType subType);
173 * @brief Creates a geometry for the grid size to be used by this visuals' shaders
175 * @param[in] gridSize The grid size of the solid geometry to create
176 * @return Returns the created geometry for the grid size
178 Geometry CreateGridGeometry(Uint16Pair gridSize);
181 * @brief Creates a geometry with the border only for the grid size to be used by this visuals' shaders
182 * e.g. a 5x4 grid would create a geometry that would look like:
184 * ---------------------
187 * ---------------------
193 * ---------------------
196 * ---------------------
198 * @param[in] gridSize The grid size of the solid geometry to create
199 * @return Returns the created geometry for the grid size
201 Geometry CreateBorderGeometry(Uint16Pair gridSize);
204 * @brief Creates a renderer by using loaded resource.
210 * @copydoc TextureUploadObserver::LoadCompleted
212 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
213 * This callback is the place to add the renderer as it would be called once the loading is finished.
215 void LoadComplete(bool loadSuccess, TextureInformation textureInformation) override;
218 WeakHandle<Actor> mPlacementActor; ///< Weakhandle to contain Actor during texture loading
219 NPatchLoader& mLoader; ///< reference to N patch loader for fast access
220 ImageVisualShaderFactory& mImageVisualShaderFactory;
221 VisualUrl mImageUrl; ///< The url to the N patch to load
222 VisualUrl mAuxiliaryUrl; ///< An auxiliary image that can be displayed on top of the N-Patch
223 NPatchData::NPatchDataId mId; ///< id of the N patch (from loader/cache)
224 Devel::PixelBuffer mAuxiliaryPixelBuffer; ///< pixel buffer of the auxiliary mask image
225 bool mBorderOnly; ///< if only border is desired
226 Rect<int> mBorder; ///< The size of the border
227 float mAuxiliaryImageAlpha; ///< The alpha value for the auxiliary image only
228 Toolkit::ImageVisual::ReleasePolicy::Type mReleasePolicy; ///< The release policy to determine when an image should no longer be cached.
231 } // namespace Internal
233 } // namespace Toolkit
237 #endif // DALI_TOOLKIT_INTERNAL_N_PATCH_VISUAL_H