1b3f098a6fd1f235f8725c03be226b29efc9c41f
[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) 2024 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/integration-api/adaptor-framework/shader-precompiler.h>
23 #include <dali/public-api/math/uint-16-pair.h>
24 #include <dali/public-api/object/ref-object.h>
25 #include <dali/public-api/rendering/geometry.h>
26 #include <dali/public-api/rendering/shader.h>
27
28 // INTERNAL INCLUDES
29 #include <dali-toolkit/internal/texture-manager/texture-manager-impl.h>
30 #include <dali-toolkit/internal/visuals/npatch-loader.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_ROUNDED_CORNER,
85     IMAGE_SHADER_BORDERLINE,
86     IMAGE_SHADER_ROUNDED_BORDERLINE,
87     IMAGE_SHADER_MASKING,
88     IMAGE_SHADER_ROUNDED_CORNER_MASKING,
89     IMAGE_SHADER_BORDERLINE_MASKING,
90     IMAGE_SHADER_ROUNDED_BORDERLINE_MASKING,
91     IMAGE_SHADER_ATLAS_DEFAULT_WRAP,
92     IMAGE_SHADER_ATLAS_CUSTOM_WRAP,
93     IMAGE_SHADER_YUV_TO_RGB,
94     IMAGE_SHADER_ROUNDED_CORNER_YUV_TO_RGB,
95     IMAGE_SHADER_BORDERLINE_YUV_TO_RGB,
96     IMAGE_SHADER_ROUNDED_BORDERLINE_YUV_TO_RGB,
97     IMAGE_SHADER_YUV_AND_RGB,
98     IMAGE_SHADER_ROUNDED_CORNER_YUV_AND_RGB,
99     IMAGE_SHADER_BORDERLINE_YUV_AND_RGB,
100     IMAGE_SHADER_ROUNDED_BORDERLINE_YUV_AND_RGB,
101     NATIVE_IMAGE_SHADER,
102     NATIVE_IMAGE_SHADER_ROUNDED_CORNER,
103     NATIVE_IMAGE_SHADER_BORDERLINE,
104     NATIVE_IMAGE_SHADER_ROUNDED_BORDERLINE,
105     NATIVE_IMAGE_SHADER_MASKING,
106     NATIVE_IMAGE_SHADER_ROUNDED_CORNER_MASKING,
107     NATIVE_IMAGE_SHADER_BORDERLINE_MASKING,
108     NATIVE_IMAGE_SHADER_ROUNDED_BORDERLINE_MASKING,
109     NATIVE_IMAGE_SHADER_ATLAS_DEFAULT_WRAP,
110     NATIVE_IMAGE_SHADER_ATLAS_CUSTOM_WRAP,
111     NINE_PATCH_SHADER,
112     NINE_PATCH_MASK_SHADER,
113     TEXT_SHADER_SINGLE_COLOR_TEXT,
114     TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE,
115     TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_OVERLAY,
116     TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_OVERLAY,
117     TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_EMOJI,
118     TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_EMOJI,
119     TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_OVERLAY_AND_EMOJI,
120     TEXT_SHADER_SINGLE_COLOR_TEXT_WITH_STYLE_AND_OVERLAY_AND_EMOJI,
121     TEXT_SHADER_MULTI_COLOR_TEXT,
122     TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE,
123     TEXT_SHADER_MULTI_COLOR_TEXT_WITH_OVERLAY,
124     TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE_AND_OVERLAY,
125     ANIMATED_GRADIENT_SHADER_LINEAR_BOUNDING_REFLECT,
126     ANIMATED_GRADIENT_SHADER_LINEAR_BOUNDING_REPEAT,
127     ANIMATED_GRADIENT_SHADER_LINEAR_BOUNDING_CLAMP,
128     ANIMATED_GRADIENT_SHADER_LINEAR_USER_REFLECT,
129     ANIMATED_GRADIENT_SHADER_LINEAR_USER_REPEAT,
130     ANIMATED_GRADIENT_SHADER_LINEAR_USER_CLAMP,
131     ANIMATED_GRADIENT_SHADER_RADIAL_BOUNDING_REFLECT,
132     ANIMATED_GRADIENT_SHADER_RADIAL_BOUNDING_REPEAT,
133     ANIMATED_GRADIENT_SHADER_RADIAL_BOUNDING_CLAMP,
134     ANIMATED_GRADIENT_SHADER_RADIAL_USER_REFLECT,
135     ANIMATED_GRADIENT_SHADER_RADIAL_USER_REPEAT,
136     ANIMATED_GRADIENT_SHADER_RADIAL_USER_CLAMP,
137     WIREFRAME_SHADER,
138     ARC_BUTT_CAP_SHADER,
139     ARC_ROUND_CAP_SHADER,
140     SHADER_TYPE_MAX = ARC_ROUND_CAP_SHADER
141   };
142
143   /**
144    * Type of geometry for caching.
145    */
146   enum GeometryType
147   {
148     QUAD_GEOMETRY,
149     BORDER_GEOMETRY,
150     NINE_PATCH_GEOMETRY,
151     NINE_PATCH_BORDER_GEOMETRY,
152     WIREFRAME_GEOMETRY,
153     GEOMETRY_TYPE_MAX = WIREFRAME_GEOMETRY
154   };
155
156 public:
157   /**
158    * @brief Constructor
159    *
160    * @param[in] preMultiplyOnLoad True if image visuals should pre-multiply alpha on image load.
161    */
162   VisualFactoryCache(bool preMultiplyOnLoad);
163
164   /**
165    * @brief Destructor
166    */
167   ~VisualFactoryCache();
168
169   /**
170    * Request geometry of the given type.
171    * @return The geometry of the required type if it exist in the cache. Otherwise, an empty handle is returned.
172    */
173   Geometry GetGeometry(GeometryType type);
174
175   /**
176    * Cache the geometry of the give type.
177    * @param[in] type The geometry type.
178    * @param[in] geometry The geometry for caching.
179    */
180   void SaveGeometry(GeometryType type, Geometry geometry);
181
182   /**
183    * Request shader of the given type.
184    * @return The shader of the required type if it exist in the cache. Otherwise, an empty handle is returned.
185    */
186   Shader GetShader(ShaderType type);
187
188   /**
189    * Generate and cache the shader of the give type. The name of shader will be installed to shader.
190    * @param[in] type The shder type.
191    * @param[in] vertexShader The vertex shader code.
192    * @param[in] fragmentShader The fragment shader code.
193    * @return The shader created by given vertex and fragment shader code.
194    */
195   Shader GenerateAndSaveShader(ShaderType type, std::string_view vertexShader, std::string_view fragmentShader);
196
197   /*
198    * Greate the quad geometry.
199    * Quad geometry is shared by multiple kind of Renderer, so implement it in the factory-cache.
200    */
201   static Geometry CreateQuadGeometry();
202
203   /**
204    * Create the grid geometry.
205    * @param[in] gridSize The size of the grid.
206    * @return The created grid geometry.
207    */
208   static Geometry CreateGridGeometry(Uint16Pair gridSize);
209
210   /**
211    * @copydoc Toolkit::VisualFactory::SetPreMultiplyOnLoad()
212    */
213   void SetPreMultiplyOnLoad(bool preMultiply);
214
215   /**
216    * @copydoc Toolkit::VisualFactory::GetPreMultiplyOnLoad()
217    */
218   bool GetPreMultiplyOnLoad() const;
219
220   /**
221    * @brief Set an image to be used when a visual has failed to correctly render
222    * @param[in] brokenImageUrlList The broken image url list
223    */
224   void SetBrokenImageUrl(std::string& defaultBrokenUrl, const std::vector<std::string>& brokenImageUrlList);
225
226   /**
227    * @brief Update the broken image Renderer object
228    * @param[in,out] renderer renderer for broken image
229    * @param[in] size the size of actor
230    * @param[in] rendererIsImage True if input renderer use image shader already.
231    *                            If true, we don't need to create new renderer when broken image is single image.
232    *                            Most of user experience use normal images. So It can reduce runtime.
233    */
234   void UpdateBrokenImageRenderer(Renderer& renderer, const Vector2& size, const bool& rendererIsImage = true);
235
236   /**
237    * @brief Get whether we support YUV Planes load or not.
238    *
239    * @return True if we support to load YUV Planes. False otherwise.
240    */
241   bool GetLoadYuvPlanes() const;
242
243 public:
244   /**
245    * Get the image atlas manager.
246    * @return A pointer to the atlas manager
247    */
248   ImageAtlasManagerPtr GetAtlasManager();
249
250   /**
251    * Get the texture manager
252    * @return A reference to the texture manager
253    */
254   TextureManager& GetTextureManager();
255
256   /**
257    * Get the N-Patch texture cache.
258    * @return A reference to the N patch loader
259    */
260   NPatchLoader& GetNPatchLoader();
261
262   /**
263    * Get the vector animation manager.
264    * @return A reference to the vector animation manager.
265    */
266   VectorAnimationManager& GetVectorAnimationManager();
267
268 protected:
269   /**
270    * Undefined copy constructor.
271    */
272   VisualFactoryCache(const VisualFactoryCache&);
273
274   /**
275    * Undefined assignment operator.
276    */
277   VisualFactoryCache& operator=(const VisualFactoryCache& rhs);
278
279 private:
280   /**
281    * @brief Returns a cached Texture to use when a visual has failed to correctly render
282    * @param[in] brokenIndex The index of broken image
283    *
284    * @return The broken image texture.
285    */
286   Texture GetBrokenVisualImage(uint32_t brokenIndex);
287
288   /**
289    * @brief Gets the Proper broken image index
290    * @param[in] size The size of actor
291    *
292    * @return The index of broken image
293    */
294   int32_t GetProperBrokenImageIndex(const Vector2& size);
295
296   /**
297    * @brief Apply a texture and uniforms
298    *
299    * @param[in,out] renderer The renderer for broken image
300    * @param[in] index The index of broken image
301    */
302   void ApplyTextureAndUniforms(Renderer& renderer, int index);
303
304   /**
305    * @brief Gets a geometry for npatch image
306    *
307    * @param[in] index the index of broken image
308    * @return The Geometry for NPatch
309    */
310   Geometry GetNPatchGeometry(int index);
311
312   /**
313    * @brief Gets the Npatch Shader object
314    *
315    * @param[in] index The index of broken image
316    * @return The Shader for NPatch
317    */
318   Shader GetNPatchShader(int index);
319
320   /**
321    * @brief Returns a broken image type
322    * @param[in] index BrokenImage index
323    * @return The broken image type.
324    */
325   VisualUrl::Type GetBrokenImageVisualType(int index);
326
327 private:
328   struct BrokenImageInfo
329   {
330     BrokenImageInfo()
331     : visualType(),
332       url(""),
333       npatchId(NPatchData::INVALID_NPATCH_DATA_ID),
334       texture(),
335       width(0),
336       height(0)
337     {
338     }
339
340     ~BrokenImageInfo()
341     {
342     }
343
344     // Data
345     VisualUrl::Type          visualType;
346     std::string              url;
347     NPatchData::NPatchDataId npatchId;
348     Texture                  texture;
349     uint32_t                 width;
350     uint32_t                 height;
351   };
352
353   Geometry mGeometry[GEOMETRY_TYPE_MAX + 1];
354   Shader   mShader[SHADER_TYPE_MAX + 1];
355
356   bool mLoadYuvPlanes; ///< A global flag to specify if the image should be loaded as yuv planes
357
358   ImageAtlasManagerPtr mAtlasManager;
359   TextureManager       mTextureManager;
360   NPatchLoader         mNPatchLoader;
361
362   std::unique_ptr<VectorAnimationManager> mVectorAnimationManager;
363   bool                                    mPreMultiplyOnLoad;
364   std::vector<BrokenImageInfo>            mBrokenImageInfoContainer;
365   std::string                             mDefaultBrokenImageUrl;
366   bool                                    mUseDefaultBrokenImageOnly;
367 };
368
369 } // namespace Internal
370
371 } // namespace Toolkit
372
373 } // namespace Dali
374
375 #endif // DALI_TOOLKIT_VISUAL_FACTORY_CACHE_H