Create Renderer when the Visual is created
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / npatch / npatch-visual.h
1 #ifndef DALI_TOOLKIT_INTERNAL_N_PATCH_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_N_PATCH_VISUAL_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/common/intrusive-ptr.h>
23 #include <dali/public-api/images/image-operations.h>
24 #include <dali/public-api/rendering/geometry.h>
25 #include <dali/public-api/rendering/sampler.h>
26 #include <dali/public-api/rendering/shader.h>
27 #include <dali/public-api/object/weak-handle.h>
28
29 // INTERNAL INCLUDES
30 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
31 #include <dali-toolkit/internal/visuals/texture-upload-observer.h>
32 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
33 #include <dali-toolkit/internal/visuals/visual-url.h>
34
35 namespace Dali
36 {
37
38 namespace Toolkit
39 {
40
41 namespace Internal
42 {
43 class ImageVisualShaderFactory;
44 class NPatchVisual;
45 typedef IntrusivePtr< NPatchVisual > NPatchVisualPtr;
46
47 /**
48  * The visual which renders an 9 patch image to the control's quad
49  *
50  * The following properties are optional
51  *
52  * | %Property Name           | Type             |
53  * |--------------------------|------------------|
54  * | url                      | STRING           |
55  * | borderOnly               | BOOLEAN          |
56  * | border                   | RECTANGLE        |
57  * | auxiliaryImage           | STRING           |
58  * | auxiliaryImageAlpha      | FLOAT            |
59  */
60 class NPatchVisual: public Visual::Base, public TextureUploadObserver
61 {
62 public:
63   /**
64    * @brief Create an N-patch visual using an image URL.
65    *
66    * The visual will load the image synchronously when the associated actor is put on stage, and destroy the image when it is off stage
67    *
68    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
69    * @param[in] shaderFactory The ImageVisualShaderFactory object
70    * @param[in] imageUrl The URL to 9 patch image resource to use
71    * @param[in] properties A Property::Map containing settings for this visual
72    * @return A smart-pointer to the newly allocated visual.
73    */
74   static NPatchVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties);
75
76   /**
77    * @brief Create an N-patch visual using an image URL.
78    *
79    * The visual will load the image synchronously when the associated actor is put on stage, and destroy the image when it is off stage
80    *
81    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
82    * @param[in] shaderFactory The ImageVisualShaderFactory object
83    * @param[in] imageUrl The URL to 9 patch image resource to use
84    * @return A smart-pointer to the newly allocated visual.
85    */
86   static NPatchVisualPtr New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl);
87
88 public:  // from Visual
89
90   /**
91    * @copydoc Visual::Base::GetNaturalSize
92    */
93   void GetNaturalSize( Vector2& naturalSize ) override;
94
95   /**
96    * @copydoc Visual::Base::CreatePropertyMap
97    */
98   void DoCreatePropertyMap( Property::Map& map ) const override;
99
100   /**
101    * @copydoc Visual::Base::CreateInstancePropertyMap
102    */
103   void DoCreateInstancePropertyMap( Property::Map& map ) const override;
104
105 protected:
106
107   /**
108    * @brief Constructor.
109    *
110    * @param[in] factoryCache Reference to the VisualFactoryCache object
111    */
112   NPatchVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory);
113
114   /**
115    * @brief A reference counted object may only be deleted by calling Unreference().
116    */
117   virtual ~NPatchVisual();
118
119   /**
120    * @copydoc Visual::Base::OnInitialize
121    */
122   void OnInitialize() override;
123
124   /**
125    * @copydoc Visual::Base::DoSetProperties
126    */
127   void DoSetProperties( const Property::Map& propertyMap ) override;
128
129   /**
130    * @copydoc Visual::Base::DoSetOnScene
131    */
132   void DoSetOnScene( Actor& actor ) override;
133
134   /**
135    * @copydoc Visual::Base::DoSetOffScene
136    */
137   void DoSetOffScene( Actor& actor ) override;
138
139   /**
140    * @copydoc Visual::Base::OnSetTransform
141    */
142   void OnSetTransform() override;
143
144 private:
145
146   /**
147    * Loads the NPatch image and the Auxiliary image if needed
148    */
149   void LoadImages();
150
151   /**
152    * @brief Creates a geometry for this renderer's grid size
153    *
154    * @return Returns the created geometry for this renderer's grid size
155    */
156   Geometry CreateGeometry();
157
158   /**
159    * @brief Creates a shader for this renderer's grid size
160    *
161    * @return Returns the created shader for this renderer's grid size
162    */
163   Shader CreateShader();
164
165   /**
166    * @brief Applies texture and related uniforms
167    */
168   void ApplyTextureAndUniforms();
169
170   /**
171    * Helper method to get the default Nine patch geometry from cache or create and store it there
172    * @param subType to use
173    * @return the geometry
174    */
175   Geometry GetNinePatchGeometry( VisualFactoryCache::GeometryType subType );
176
177   /**
178    * @brief Creates a geometry for the grid size to be used by this visuals' shaders
179    *
180    * @param[in] gridSize The grid size of the solid geometry to create
181    * @return Returns the created geometry for the grid size
182    */
183   Geometry CreateGridGeometry( Uint16Pair gridSize );
184
185   /**
186    * @brief Creates a geometry with the border only for the grid size to be used by this visuals' shaders
187    * e.g. a 5x4 grid would create a geometry that would look like:
188    *
189    *   ---------------------
190    *   |  /|  /|  /|  /|  /|
191    *   |/  |/  |/  |/  |/  |
192    *   ---------------------
193    *   |  /|           |  /|
194    *   |/  |           |/  |
195    *   -----           -----
196    *   |  /|           |  /|
197    *   |/  |           |/  |
198    *   ---------------------
199    *   |  /|  /|  /|  /|  /|
200    *   |/  |/  |/  |/  |/  |
201    *   ---------------------
202    *
203    * @param[in] gridSize The grid size of the solid geometry to create
204    * @return Returns the created geometry for the grid size
205    */
206   Geometry CreateBorderGeometry( Uint16Pair gridSize );
207
208   /**
209    * @brief Creates a renderer by using loaded resource.
210    */
211   void SetResource();
212
213 private:
214
215   /**
216    * @copydoc TextureUploadObserver::UploadCompleted
217    *
218    * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
219    * This callback is the place to add the renderer as it would be called once the loading is finished.
220    */
221   void UploadComplete( bool loadSuccess, int32_t textureId, TextureSet textureSet, bool useAtlasing, const Vector4& atlasRect, bool preMultiplied ) override;
222
223   /**
224    * @copydoc TextureUploadObserver::LoadComplete
225    *
226    * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
227    * This callback is the place to add the renderer as it would be called once the loading is finished.
228    */
229   void LoadComplete( bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied ) override;
230
231 private:
232   WeakHandle<Actor>                         mPlacementActor; ///< Weakhandle to contain Actor during texture loading
233   NPatchLoader&                             mLoader;         ///< reference to N patch loader for fast access
234   ImageVisualShaderFactory&                 mImageVisualShaderFactory;
235   VisualUrl                                 mImageUrl;             ///< The url to the N patch to load
236   VisualUrl                                 mAuxiliaryUrl;         ///< An auxiliary image that can be displayed on top of the N-Patch
237   NPatchData::NPatchDataId                  mId;                   ///< id of the N patch (from loader/cache)
238   Devel::PixelBuffer                        mAuxiliaryPixelBuffer; ///< pixel buffer of the auxiliary mask image
239   bool                                      mBorderOnly;           ///< if only border is desired
240   Rect<int>                                 mBorder;               ///< The size of the border
241   float                                     mAuxiliaryImageAlpha;  ///< The alpha value for the auxiliary image only
242   Toolkit::ImageVisual::ReleasePolicy::Type mReleasePolicy; ///< The release policy to determine when an image should no longer be cached.
243 };
244
245 } // namespace Internal
246
247 } // namespace Toolkit
248
249 } // namespace Dali
250
251 #endif // DALI_TOOLKIT_INTERNAL_N_PATCH_VISUAL_H