1 #ifndef DALI_TOOLKIT_VISUAL_FACTORY_CACHE_H
2 #define DALI_TOOLKIT_VISUAL_FACTORY_CACHE_H
5 * Copyright (c) 2024 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
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>
41 class ImageAtlasManager;
44 class VectorAnimationManager;
46 typedef IntrusivePtr<ImageAtlasManager> ImageAtlasManagerPtr;
49 * Caches shaders and geometries. Owned by VisualFactory.
51 class VisualFactoryCache
55 * Type of shader for caching.
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,
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,
84 IMAGE_SHADER_ROUNDED_CORNER,
85 IMAGE_SHADER_BORDERLINE,
86 IMAGE_SHADER_ROUNDED_BORDERLINE,
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,
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,
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,
137 ARC_ROUND_CAP_SHADER,
138 SHADER_TYPE_MAX = ARC_ROUND_CAP_SHADER
142 * Type of geometry for caching.
149 NINE_PATCH_BORDER_GEOMETRY,
151 GEOMETRY_TYPE_MAX = WIREFRAME_GEOMETRY
158 * @param[in] preMultiplyOnLoad True if image visuals should pre-multiply alpha on image load.
160 VisualFactoryCache(bool preMultiplyOnLoad);
165 ~VisualFactoryCache();
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.
171 Geometry GetGeometry(GeometryType type);
174 * Cache the geometry of the give type.
175 * @param[in] type The geometry type.
176 * @param[in] geometry The geometry for caching.
178 void SaveGeometry(GeometryType type, Geometry geometry);
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.
184 Shader GetShader(ShaderType type);
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.
193 Shader GenerateAndSaveShader(ShaderType type, std::string_view vertexShader, std::string_view fragmentShader);
196 * Greate the quad geometry.
197 * Quad geometry is shared by multiple kind of Renderer, so implement it in the factory-cache.
199 static Geometry CreateQuadGeometry();
202 * Create the grid geometry.
203 * @param[in] gridSize The size of the grid.
204 * @return The created grid geometry.
206 static Geometry CreateGridGeometry(Uint16Pair gridSize);
209 * @copydoc Toolkit::VisualFactory::SetPreMultiplyOnLoad()
211 void SetPreMultiplyOnLoad(bool preMultiply);
214 * @copydoc Toolkit::VisualFactory::GetPreMultiplyOnLoad()
216 bool GetPreMultiplyOnLoad() const;
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
222 void SetBrokenImageUrl(std::string& defaultBrokenUrl, const std::vector<std::string>& brokenImageUrlList);
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.
232 void UpdateBrokenImageRenderer(Renderer& renderer, const Vector2& size, const bool& rendererIsImage = true);
235 * @brief Get whether we support YUV Planes load or not.
237 * @return True if we support to load YUV Planes. False otherwise.
239 bool GetLoadYuvPlanes() const;
243 * Get the image atlas manager.
244 * @return A pointer to the atlas manager
246 ImageAtlasManagerPtr GetAtlasManager();
249 * Get the texture manager
250 * @return A reference to the texture manager
252 TextureManager& GetTextureManager();
255 * Get the N-Patch texture cache.
256 * @return A reference to the N patch loader
258 NPatchLoader& GetNPatchLoader();
261 * Get the vector animation manager.
262 * @return A reference to the vector animation manager.
264 VectorAnimationManager& GetVectorAnimationManager();
268 * Undefined copy constructor.
270 VisualFactoryCache(const VisualFactoryCache&);
273 * Undefined assignment operator.
275 VisualFactoryCache& operator=(const VisualFactoryCache& rhs);
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
282 * @return The broken image texture.
284 Texture GetBrokenVisualImage(uint32_t brokenIndex);
287 * @brief Gets the Proper broken image index
288 * @param[in] size The size of actor
290 * @return The index of broken image
292 int32_t GetProperBrokenImageIndex(const Vector2& size);
295 * @brief Apply a texture and uniforms
297 * @param[in,out] renderer The renderer for broken image
298 * @param[in] index The index of broken image
300 void ApplyTextureAndUniforms(Renderer& renderer, int index);
303 * @brief Gets a geometry for npatch image
305 * @param[in] index the index of broken image
306 * @return The Geometry for NPatch
308 Geometry GetNPatchGeometry(int index);
311 * @brief Gets the Npatch Shader object
313 * @param[in] index The index of broken image
314 * @return The Shader for NPatch
316 Shader GetNPatchShader(int index);
319 * @brief Returns a broken image type
320 * @param[in] index BrokenImage index
321 * @return The broken image type.
323 VisualUrl::Type GetBrokenImageVisualType(int index);
326 struct BrokenImageInfo
331 npatchId(NPatchData::INVALID_NPATCH_DATA_ID),
343 VisualUrl::Type visualType;
345 NPatchData::NPatchDataId npatchId;
351 Geometry mGeometry[GEOMETRY_TYPE_MAX + 1];
352 Shader mShader[SHADER_TYPE_MAX + 1];
354 bool mLoadYuvPlanes; ///< A global flag to specify if the image should be loaded as yuv planes
356 ImageAtlasManagerPtr mAtlasManager;
357 TextureManager mTextureManager;
358 NPatchLoader mNPatchLoader;
360 std::unique_ptr<VectorAnimationManager> mVectorAnimationManager;
361 bool mPreMultiplyOnLoad;
362 std::vector<BrokenImageInfo> mBrokenImageInfoContainer;
363 std::string mDefaultBrokenImageUrl;
364 bool mUseDefaultBrokenImageOnly;
367 } // namespace Internal
369 } // namespace Toolkit
373 #endif // DALI_TOOLKIT_VISUAL_FACTORY_CACHE_H