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