1 #ifndef DALI_TOOLKIT_VISUAL_FACTORY_CACHE_H
2 #define DALI_TOOLKIT_VISUAL_FACTORY_CACHE_H
5 * Copyright (c) 2021 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/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>
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>
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_ATLAS_DEFAULT_WRAP,
85 IMAGE_SHADER_ATLAS_CUSTOM_WRAP,
86 IMAGE_SHADER_ROUNDED_CORNER,
87 IMAGE_SHADER_BORDERLINE,
88 IMAGE_SHADER_ROUNDED_BORDERLINE,
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,
111 ARC_ROUND_CAP_SHADER,
112 SHADER_TYPE_MAX = ARC_ROUND_CAP_SHADER
116 * Type of geometry for caching.
123 NINE_PATCH_BORDER_GEOMETRY,
125 GEOMETRY_TYPE_MAX = WIREFRAME_GEOMETRY
129 * @brief The Type of BrokenImage
143 * @param[in] preMultiplyOnLoad True if image visuals should pre-multiply alpha on image load.
145 VisualFactoryCache(bool preMultiplyOnLoad);
150 ~VisualFactoryCache();
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.
156 Geometry GetGeometry(GeometryType type);
159 * Cache the geometry of the give type.
160 * @param[in] type The geometry type.
161 * @param[in] geometry The geometry for caching.
163 void SaveGeometry(GeometryType type, Geometry geometry);
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.
169 Shader GetShader(ShaderType type);
172 * Cache the geometry of the give type.
173 * @param[in] type The geometry type.
174 * @param[in] geometry The geometry for caching.
176 void SaveShader(ShaderType type, Shader shader);
179 * Greate the quad geometry.
180 * Quad geometry is shared by multiple kind of Renderer, so implement it in the factory-cache.
182 static Geometry CreateQuadGeometry();
185 * Create the grid geometry.
186 * @param[in] gridSize The size of the grid.
187 * @return The created grid geometry.
189 static Geometry CreateGridGeometry(Uint16Pair gridSize);
192 * @copydoc Toolkit::VisualFactory::SetPreMultiplyOnLoad()
194 void SetPreMultiplyOnLoad(bool preMultiply);
197 * @copydoc Toolkit::VisualFactory::GetPreMultiplyOnLoad()
199 bool GetPreMultiplyOnLoad();
202 * @brief Enable a custom broken image
204 * @param[in] enabled True if cache use custom broken image
206 void EnableCustomBrokenImage(bool enabled);
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
213 void SetBrokenImageUrl(BrokenImageType brokenImageType, const std::string& brokenImageUrl);
216 * @brief Returns a broken image type
217 * @param[in] brokenImageType BrokenImage type
218 * @return The broken image type.
220 VisualUrl::Type GetBrokenImageVisualType(BrokenImageType brokenImageType);
223 * @brief Get the Npatch Renderer object
224 * @param[in] renderer renderer for npatch
225 * @param[in] size the size of actor
227 void GetBrokenImageRenderer(Renderer& renderer, const Vector2& size);
231 * Get the image atlas manager.
232 * @return A pointer to the atlas manager
234 ImageAtlasManagerPtr GetAtlasManager();
237 * Get the texture manager
238 * @return A reference to the texture manager
240 TextureManager& GetTextureManager();
243 * Get the N-Patch texture cache.
244 * @return A reference to the N patch loader
246 NPatchLoader& GetNPatchLoader();
249 * Get the SVG rasterization thread.
250 * @return A raw pointer pointing to the SVG rasterization thread.
252 SvgRasterizeThread* GetSVGRasterizationThread();
255 * Get the vector animation manager.
256 * @return A reference to the vector animation manager.
258 VectorAnimationManager& GetVectorAnimationManager();
262 * Undefined copy constructor.
264 VisualFactoryCache(const VisualFactoryCache&);
267 * Undefined assignment operator.
269 VisualFactoryCache& operator=(const VisualFactoryCache& rhs);
273 * @brief Returns a new Texture to use when a visual has failed to correctly render
274 * @return The broken image texture.
276 Texture GetBrokenVisualImage(BrokenImageType brokenImageType);
279 * @brief Change the Proper broken image type
280 * @param[in] size The size of actor
282 * @return The type of broken image
284 BrokenImageType GetProperBrokenImageType(const Vector2& size);
287 * @brief Apply a texture and uniforms
289 * @param renderer The renderer for broken image
290 * @param brokenImageType The type of broken image
292 void ApplyTextureAndUniforms(Renderer& renderer, BrokenImageType brokenImageType);
295 * @brief Creates a Npatch Geometry object
297 * @param[in] gridSize The gridSize for creating a geometry
298 * @return The Geometry for NPatch
300 Geometry CreateNPatchGeometry(Uint16Pair gridSize);
303 * @brief Gets a geometry for npatch image
305 * @param[in] brokenImageType the type of broken image
306 * @return The Geometry for NPatch
308 Geometry GetNPatchGeometry(BrokenImageType brokenImageType);
311 * @brief Gets the Npatch Shader object
313 * @param[in] brokenImageType The type of broken image
314 * @return The Shader for NPatch
316 Shader GetNPatchShader(BrokenImageType brokenImageType);
319 * @brief Registers a properties for Stretch Ranges
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
327 void RegisterStretchProperties(Renderer& renderer, BrokenImageType brokenImageType, const char* uniformName, const NPatchUtility::StretchRanges& stretchPixels, uint16_t imageExtent);
330 struct BrokenImageInfo
335 npatchId(NPatchData::INVALID_NPATCH_DATA_ID),
347 VisualUrl::Type visualType;
349 NPatchData::NPatchDataId npatchId;
355 Geometry mGeometry[GEOMETRY_TYPE_MAX + 1];
356 Shader mShader[SHADER_TYPE_MAX + 1];
358 ImageAtlasManagerPtr mAtlasManager;
359 TextureManager mTextureManager;
360 NPatchLoader mNPatchLoader;
362 SvgRasterizeThread* mSvgRasterizeThread;
363 std::unique_ptr<VectorAnimationManager> mVectorAnimationManager;
364 bool mPreMultiplyOnLoad;
365 std::vector<BrokenImageInfo> mBrokenImageInfoContainer;
366 bool mUseCustomBrokenImage;
369 } // namespace Internal
371 } // namespace Toolkit
375 #endif // DALI_TOOLKIT_VISUAL_FACTORY_CACHE_H