d7e390f71d91ffc6e0bf9d03c7fa4882a69567df
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-factory-cache.h
1 #ifndef DALI_TOOLKIT_VISUAL_FACTORY_CACHE_H
2 #define DALI_TOOLKIT_VISUAL_FACTORY_CACHE_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 // EXTERNAL INCLUDES
21 #include <dali/devel-api/common/owner-container.h>
22 #include <dali/public-api/math/uint-16-pair.h>
23 #include <dali/public-api/object/ref-object.h>
24 #include <dali/public-api/rendering/geometry.h>
25 #include <dali/public-api/rendering/shader.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/internal/visuals/npatch-loader.h>
29 #include <dali-toolkit/internal/visuals/svg/svg-rasterize-thread.h>
30 #include <dali-toolkit/internal/visuals/texture-manager-impl.h>
31 #include <dali/devel-api/rendering/renderer-devel.h>
32
33 namespace Dali
34 {
35 class Texture;
36
37 namespace Toolkit
38 {
39 namespace Internal
40 {
41 class ImageAtlasManager;
42 class NPatchLoader;
43 class TextureManager;
44 class VectorAnimationManager;
45
46 typedef IntrusivePtr<ImageAtlasManager> ImageAtlasManagerPtr;
47
48 /**
49  * Caches shaders and geometries. Owned by VisualFactory.
50  */
51 class VisualFactoryCache
52 {
53 public:
54   /**
55    * Type of shader for caching.
56    */
57   enum ShaderType
58   {
59     COLOR_SHADER,
60     COLOR_SHADER_ROUNDED_CORNER,
61     COLOR_SHADER_BORDERLINE,
62     COLOR_SHADER_ROUNDED_BORDERLINE,
63     COLOR_SHADER_BLUR_EDGE,
64     COLOR_SHADER_ROUNDED_CORNER_BLUR_EDGE,
65     BORDER_SHADER,
66     BORDER_SHADER_ANTI_ALIASING,
67     GRADIENT_SHADER_LINEAR_BOUNDING_BOX,
68     GRADIENT_SHADER_LINEAR_BOUNDING_BOX_ROUNDED_CORNER,
69     GRADIENT_SHADER_LINEAR_BOUNDING_BOX_BORDERLINE,
70     GRADIENT_SHADER_LINEAR_BOUNDING_BOX_ROUNDED_BORDERLINE,
71     GRADIENT_SHADER_LINEAR_USER_SPACE,
72     GRADIENT_SHADER_LINEAR_USER_SPACE_ROUNDED_CORNER,
73     GRADIENT_SHADER_LINEAR_USER_SPACE_BORDERLINE,
74     GRADIENT_SHADER_LINEAR_USER_SPACE_ROUNDED_BORDERLINE,
75     GRADIENT_SHADER_RADIAL_BOUNDING_BOX,
76     GRADIENT_SHADER_RADIAL_BOUNDING_BOX_ROUNDED_CORNER,
77     GRADIENT_SHADER_RADIAL_BOUNDING_BOX_BORDERLINE,
78     GRADIENT_SHADER_RADIAL_BOUNDING_BOX_ROUNDED_BORDERLINE,
79     GRADIENT_SHADER_RADIAL_USER_SPACE,
80     GRADIENT_SHADER_RADIAL_USER_SPACE_ROUNDED_CORNER,
81     GRADIENT_SHADER_RADIAL_USER_SPACE_BORDERLINE,
82     GRADIENT_SHADER_RADIAL_USER_SPACE_ROUNDED_BORDERLINE,
83     IMAGE_SHADER,
84     IMAGE_SHADER_ATLAS_DEFAULT_WRAP,
85     IMAGE_SHADER_ATLAS_CUSTOM_WRAP,
86     IMAGE_SHADER_ROUNDED_CORNER,
87     IMAGE_SHADER_BORDERLINE,
88     IMAGE_SHADER_ROUNDED_BORDERLINE,
89     NINE_PATCH_SHADER,
90     NINE_PATCH_MASK_SHADER,
91     TEXT_SHADER_MULTI_COLOR_TEXT,
92     TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE,
93     TEXT_SHADER_SINGLE_COLOR_TEXT,
94     TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE,
95     TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI,
96     TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI,
97     ANIMATED_GRADIENT_SHADER_LINEAR_BOUNDING_REFLECT,
98     ANIMATED_GRADIENT_SHADER_LINEAR_BOUNDING_REPEAT,
99     ANIMATED_GRADIENT_SHADER_LINEAR_BOUNDING_CLAMP,
100     ANIMATED_GRADIENT_SHADER_LINEAR_USER_REFLECT,
101     ANIMATED_GRADIENT_SHADER_LINEAR_USER_REPEAT,
102     ANIMATED_GRADIENT_SHADER_LINEAR_USER_CLAMP,
103     ANIMATED_GRADIENT_SHADER_RADIAL_BOUNDING_REFLECT,
104     ANIMATED_GRADIENT_SHADER_RADIAL_BOUNDING_REPEAT,
105     ANIMATED_GRADIENT_SHADER_RADIAL_BOUNDING_CLAMP,
106     ANIMATED_GRADIENT_SHADER_RADIAL_USER_REFLECT,
107     ANIMATED_GRADIENT_SHADER_RADIAL_USER_REPEAT,
108     ANIMATED_GRADIENT_SHADER_RADIAL_USER_CLAMP,
109     WIREFRAME_SHADER,
110     ARC_BUTT_CAP_SHADER,
111     ARC_ROUND_CAP_SHADER,
112     SHADER_TYPE_MAX = ARC_ROUND_CAP_SHADER
113   };
114
115   /**
116    * Type of geometry for caching.
117    */
118   enum GeometryType
119   {
120     QUAD_GEOMETRY,
121     BORDER_GEOMETRY,
122     NINE_PATCH_GEOMETRY,
123     NINE_PATCH_BORDER_GEOMETRY,
124     WIREFRAME_GEOMETRY,
125     GEOMETRY_TYPE_MAX = WIREFRAME_GEOMETRY
126   };
127
128   /**
129    * @brief The Type of BrokenImage
130    */
131   enum BrokenImageType
132   {
133     SMALL,
134     NORMAL,
135     LARGE
136   };
137
138 public:
139
140   /**
141    * @brief Constructor
142    *
143    * @param[in] preMultiplyOnLoad True if image visuals should pre-multiply alpha on image load.
144    */
145   VisualFactoryCache(bool preMultiplyOnLoad);
146
147   /**
148    * @brief Destructor
149    */
150   ~VisualFactoryCache();
151
152   /**
153    * Request geometry of the given type.
154    * @return The geometry of the required type if it exist in the cache. Otherwise, an empty handle is returned.
155    */
156   Geometry GetGeometry(GeometryType type);
157
158   /**
159    * Cache the geometry of the give type.
160    * @param[in] type The geometry type.
161    * @param[in] geometry The geometry for caching.
162    */
163   void SaveGeometry(GeometryType type, Geometry geometry);
164
165   /**
166    * Request shader of the given type.
167    * @return The shader of the required type if it exist in the cache. Otherwise, an empty handle is returned.
168    */
169   Shader GetShader(ShaderType type);
170
171   /**
172    * Cache the geometry of the give type.
173    * @param[in] type The geometry type.
174    * @param[in] geometry The geometry for caching.
175    */
176   void SaveShader(ShaderType type, Shader shader);
177
178   /*
179    * Greate the quad geometry.
180    * Quad geometry is shared by multiple kind of Renderer, so implement it in the factory-cache.
181    */
182   static Geometry CreateQuadGeometry();
183
184   /**
185    * Create the grid geometry.
186    * @param[in] gridSize The size of the grid.
187    * @return The created grid geometry.
188    */
189   static Geometry CreateGridGeometry(Uint16Pair gridSize);
190
191   /**
192    * @copydoc Toolkit::VisualFactory::SetPreMultiplyOnLoad()
193    */
194   void SetPreMultiplyOnLoad(bool preMultiply);
195
196   /**
197    * @copydoc Toolkit::VisualFactory::GetPreMultiplyOnLoad()
198    */
199   bool GetPreMultiplyOnLoad();
200
201   /**
202    * @brief Enable a custom broken image
203    *
204    * @param[in] enabled True if cache use custom broken image
205    */
206   void EnableCustomBrokenImage(bool enabled);
207
208   /**
209    * @brief Set an image to be used when a visual has failed to correctly render
210    * @param[in] brokenImageType BrokenImage type
211    * @param[in] brokenImageUrl The broken image url
212    */
213   void SetBrokenImageUrl(BrokenImageType brokenImageType, const std::string& brokenImageUrl);
214
215   /**
216    * @brief Returns a broken image type
217    * @param[in] brokenImageType BrokenImage type
218    * @return The broken image type.
219    */
220   VisualUrl::Type GetBrokenImageVisualType(BrokenImageType brokenImageType);
221
222   /**
223    * @brief Get the Npatch Renderer object
224    * @param[in] renderer renderer for npatch
225    * @param[in] size     the size of actor
226    */
227   void GetBrokenImageRenderer(Renderer& renderer, const Vector2& size);
228
229 public:
230   /**
231    * Get the image atlas manager.
232    * @return A pointer to the atlas manager
233    */
234   ImageAtlasManagerPtr GetAtlasManager();
235
236   /**
237    * Get the texture manager
238    * @return A reference to the texture manager
239    */
240   TextureManager& GetTextureManager();
241
242   /**
243    * Get the N-Patch texture cache.
244    * @return A reference to the N patch loader
245    */
246   NPatchLoader& GetNPatchLoader();
247
248   /**
249    * Get the SVG rasterization thread.
250    * @return A raw pointer pointing to the SVG rasterization thread.
251    */
252   SvgRasterizeThread* GetSVGRasterizationThread();
253
254   /**
255    * Get the vector animation manager.
256    * @return A reference to the vector animation manager.
257    */
258   VectorAnimationManager& GetVectorAnimationManager();
259
260 protected:
261   /**
262    * Undefined copy constructor.
263    */
264   VisualFactoryCache(const VisualFactoryCache&);
265
266   /**
267    * Undefined assignment operator.
268    */
269   VisualFactoryCache& operator=(const VisualFactoryCache& rhs);
270
271 private:
272   /**
273    * @brief Returns a new Texture to use when a visual has failed to correctly render
274    * @return The broken image texture.
275    */
276   Texture GetBrokenVisualImage(BrokenImageType brokenImageType);
277
278   /**
279    * @brief Change the Proper broken image type
280    * @param[in] size The size of actor
281    *
282    * @return The type of broken image
283    */
284   BrokenImageType GetProperBrokenImageType(const Vector2& size);
285
286   /**
287    * @brief Apply a texture and uniforms
288    *
289    * @param renderer The renderer for broken image
290    * @param brokenImageType The type of broken image
291    */
292   void ApplyTextureAndUniforms(Renderer& renderer, BrokenImageType brokenImageType);
293
294   /**
295    * @brief Creates a Npatch Geometry object
296    *
297    * @param[in] gridSize The gridSize for creating a geometry
298    * @return The Geometry for NPatch
299    */
300   Geometry CreateNPatchGeometry(Uint16Pair gridSize);
301
302   /**
303    * @brief Gets a geometry for npatch image
304    *
305    * @param[in] brokenImageType the type of broken image
306    * @return The Geometry for NPatch
307    */
308   Geometry GetNPatchGeometry(BrokenImageType brokenImageType);
309
310   /**
311    * @brief Gets the Npatch Shader object
312    *
313    * @param[in] brokenImageType The type of broken image
314    * @return The Shader for NPatch
315    */
316   Shader GetNPatchShader(BrokenImageType brokenImageType);
317
318   /**
319    * @brief Registers a properties for Stretch Ranges
320    *
321    * @param renderer The renderer for broken image
322    * @param brokenImageType The type of broken image
323    * @param uniformName The name of the uniform
324    * @param stretchPixels The stretchable pixels in the cropped image space
325    * @param imageExtent The imageExtent
326    */
327   void RegisterStretchProperties(Renderer& renderer, BrokenImageType brokenImageType, const char* uniformName, const NPatchUtility::StretchRanges& stretchPixels, uint16_t imageExtent);
328
329 private:
330   struct BrokenImageInfo
331   {
332     BrokenImageInfo()
333     :visualType(),
334      url(""),
335      npatchId(NPatchData::INVALID_NPATCH_DATA_ID),
336      texture(),
337      width(0),
338      height(0)
339     {
340     }
341
342     ~BrokenImageInfo()
343     {
344     }
345
346     // Data
347     VisualUrl::Type                         visualType;
348     std::string                             url;
349     NPatchData::NPatchDataId                npatchId;
350     Texture                                 texture;
351     uint32_t                                width;
352     uint32_t                                height;
353   };
354
355   Geometry mGeometry[GEOMETRY_TYPE_MAX + 1];
356   Shader   mShader[SHADER_TYPE_MAX + 1];
357
358   ImageAtlasManagerPtr                    mAtlasManager;
359   TextureManager                          mTextureManager;
360   NPatchLoader                            mNPatchLoader;
361
362   SvgRasterizeThread*                     mSvgRasterizeThread;
363   std::unique_ptr<VectorAnimationManager> mVectorAnimationManager;
364   bool                                    mPreMultiplyOnLoad;
365   std::vector<BrokenImageInfo>            mBrokenImageInfoContainer;
366   bool                                    mUseCustomBrokenImage;
367 };
368
369 } // namespace Internal
370
371 } // namespace Toolkit
372
373 } // namespace Dali
374
375 #endif // DALI_TOOLKIT_VISUAL_FACTORY_CACHE_H