Merge "Make -DUSE_DEFAULT_RESOURCE_DIR=OFF compile again" into devel/master
[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) 2020 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
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   /**
65    * @brief Create an N-patch visual using an image URL.
66    *
67    * The visual will load the image synchronously when the associated actor is put on stage, and destroy the image when it is off stage
68    *
69    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache 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, 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] imageUrl The URL to 9 patch image resource to use
83    * @return A smart-pointer to the newly allocated visual.
84    */
85   static NPatchVisualPtr New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl );
86
87 public:  // from Visual
88
89   /**
90    * @copydoc Visual::Base::GetNaturalSize
91    */
92   void GetNaturalSize( Vector2& naturalSize ) override;
93
94   /**
95    * @copydoc Visual::Base::CreatePropertyMap
96    */
97   void DoCreatePropertyMap( Property::Map& map ) const override;
98
99   /**
100    * @copydoc Visual::Base::CreateInstancePropertyMap
101    */
102   void DoCreateInstancePropertyMap( Property::Map& map ) const override;
103
104 protected:
105
106   /**
107    * @brief Constructor.
108    *
109    * @param[in] factoryCache Reference to the VisualFactoryCache object
110    */
111   NPatchVisual( VisualFactoryCache& factoryCache );
112
113   /**
114    * @brief A reference counted object may only be deleted by calling Unreference().
115    */
116   virtual ~NPatchVisual();
117
118   /**
119    * @copydoc Visual::Base::DoSetProperties
120    */
121   void DoSetProperties( const Property::Map& propertyMap ) override;
122
123   /**
124    * @copydoc Visual::Base::DoSetOnScene
125    */
126   void DoSetOnScene( Actor& actor ) override;
127
128   /**
129    * @copydoc Visual::Base::DoSetOffScene
130    */
131   void DoSetOffScene( Actor& actor ) override;
132
133   /**
134    * @copydoc Visual::Base::OnSetTransform
135    */
136   void OnSetTransform() override;
137
138 private:
139
140   /**
141    * Loads the NPatch image and the Auxiliary image if needed
142    */
143   void LoadImages();
144
145   /**
146    * @brief Creates a geometry for this renderer's grid size
147    *
148    * @return Returns the created geometry for this renderer's grid size
149    */
150   Geometry CreateGeometry();
151
152   /**
153    * @brief Creates a shader for this renderer's grid size
154    *
155    * @return Returns the created shader for this renderer's grid size
156    */
157   Shader CreateShader();
158
159   /**
160    * @brief Applies texture and related uniforms
161    */
162   void ApplyTextureAndUniforms();
163
164   /**
165    * Helper method to get the default Nine patch geometry from cache or create and store it there
166    * @param subType to use
167    * @return the geometry
168    */
169   Geometry GetNinePatchGeometry( VisualFactoryCache::GeometryType subType );
170
171   /**
172    * @brief Creates a geometry for the grid size to be used by this visuals' shaders
173    *
174    * @param[in] gridSize The grid size of the solid geometry to create
175    * @return Returns the created geometry for the grid size
176    */
177   Geometry CreateGridGeometry( Uint16Pair gridSize );
178
179   /**
180    * @brief Creates a geometry with the border only for the grid size to be used by this visuals' shaders
181    * e.g. a 5x4 grid would create a geometry that would look like:
182    *
183    *   ---------------------
184    *   |  /|  /|  /|  /|  /|
185    *   |/  |/  |/  |/  |/  |
186    *   ---------------------
187    *   |  /|           |  /|
188    *   |/  |           |/  |
189    *   -----           -----
190    *   |  /|           |  /|
191    *   |/  |           |/  |
192    *   ---------------------
193    *   |  /|  /|  /|  /|  /|
194    *   |/  |/  |/  |/  |/  |
195    *   ---------------------
196    *
197    * @param[in] gridSize The grid size of the solid geometry to create
198    * @return Returns the created geometry for the grid size
199    */
200   Geometry CreateBorderGeometry( Uint16Pair gridSize );
201
202   /**
203    * @brief Creates a renderer by using loaded resource.
204    */
205   void SetResource();
206
207 private:
208
209   /**
210    * @copydoc TextureUploadObserver::UploadCompleted
211    *
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.
214    */
215   void UploadComplete( bool loadSuccess, int32_t textureId, TextureSet textureSet, bool useAtlasing, const Vector4& atlasRect, bool preMultiplied ) override;
216
217   /**
218    * @copydoc TextureUploadObserver::LoadComplete
219    *
220    * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
221    * This callback is the place to add the renderer as it would be called once the loading is finished.
222    */
223   void LoadComplete( bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied ) override;
224
225 private:
226   WeakHandle<Actor>        mPlacementActor;                 ///< Weakhandle to contain Actor during texture loading
227   NPatchLoader&            mLoader;                         ///< reference to N patch loader for fast access
228   VisualUrl                mImageUrl;                       ///< The url to the N patch to load
229   VisualUrl                mAuxiliaryUrl;                   ///< An auxiliary image that can be displayed on top of the N-Patch
230   NPatchData::NPatchDataId mId;                             ///< id of the N patch (from loader/cache)
231   Devel::PixelBuffer       mAuxiliaryPixelBuffer;           ///< pixel buffer of the auxiliary mask image
232   bool                     mBorderOnly;                     ///< if only border is desired
233   Rect<int>                mBorder;                         ///< The size of the border
234   float                    mAuxiliaryImageAlpha;            ///< The alpha value for the auxiliary image only
235   Toolkit::ImageVisual::ReleasePolicy::Type mReleasePolicy; ///< The release policy to determine when an image should no longer be cached.
236 };
237
238 } // namespace Internal
239
240 } // namespace Toolkit
241
242 } // namespace Dali
243
244 #endif // DALI_TOOLKIT_INTERNAL_N_PATCH_VISUAL_H