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