1 #ifndef DALI_TOOLKIT_TEXTURE_CACHE_MANAGER_H
2 #define DALI_TOOLKIT_TEXTURE_CACHE_MANAGER_H
5 * Copyright (c) 2023 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/free-list.h>
22 #include <dali/public-api/adaptor-framework/encoded-image-buffer.h>
23 #include <unordered_map>
26 #include <dali-toolkit/internal/texture-manager/texture-manager-type.h>
27 #include <dali-toolkit/internal/texture-manager/texture-upload-observer.h>
28 #include <dali-toolkit/internal/visuals/visual-url.h>
37 * @brief The contain and managing cached textures.
38 * Each Texture hold there TextureId. These TextureId can be used outside of TextureManager.
39 * Internally, each cached texture can be accessed by TextureCacheIndex.
40 * You can Convert TextureId into TextureCacheIndex by this class.
42 * Also, You can store external TextureSet or EncodedImageBuffer here.
44 * There are 3 type of CachedContainer in this manager
45 * - mTextureInfoContainer : Cache all kind of textures that need some load/upload jobs.
46 * All kind of images that visual using (not vector image) will be stored here.
47 * This container will use TEXTURE_CACHE_INDEX_TYPE_LOCAL
49 * - mExternalTextures : External appended TextureSet cache container.
50 * External TextureSet can be Something like NativeImageSource, FrameBuffer and PixelData.
51 * This container will use TEXTURE_CACHE_INDEX_TYPE_TEXTURE
52 * The textureId will be used for VisualUrl. ex) dali://1
54 * - mEncodedImageBuffers : External appended EncodedImageBuffer cache container.
55 * This container will use TEXTURE_CACHE_INDEX_TYPE_BUFFER
56 * The bufferId will be used for VisualUrl. ex) enbuf://1
57 * Note that this bufferId is not equal with textureId in mTextureInfoContainer.
59 class TextureCacheManager
62 // Copy enum and types and const values that TextureCacheManager will use.
63 using TextureCacheIndexType = TextureManagerType::TextureCacheIndexType;
64 using TextureCacheIndexData = TextureManagerType::TextureCacheIndexData;
66 using TextureId = TextureManagerType::TextureId;
67 using TextureCacheIndex = TextureManagerType::TextureCacheIndex;
68 using TextureHash = TextureManagerType::TextureHash;
70 static constexpr TextureId INVALID_TEXTURE_ID = TextureManagerType::INVALID_TEXTURE_ID;
71 static constexpr TextureCacheIndex INVALID_CACHE_INDEX = TextureManagerType::INVALID_CACHE_INDEX;
73 using UseAtlas = TextureManagerType::UseAtlas;
74 using StorageType = TextureManagerType::StorageType;
75 using LoadType = TextureManagerType::LoadType;
76 using LoadState = TextureManagerType::LoadState;
77 using ReloadPolicy = TextureManagerType::ReloadPolicy;
78 using MultiplyOnLoad = TextureManagerType::MultiplyOnLoad;
79 using TextureInfo = TextureManagerType::TextureInfo;
80 using ExternalTextureInfo = TextureManagerType::ExternalTextureInfo;
86 TextureCacheManager();
91 ~TextureCacheManager();
94 // TextureCacheManager Main API:
97 * @brief Get the visualUrl associated with the texture id.
98 * @param[in] textureId The texture Id to get
99 * @return The visual Url associated with the texture id.
101 VisualUrl GetVisualUrl(const TextureCacheManager::TextureId& textureId);
104 * @brief Get the current state of a texture
105 * @note This API doesn't consider encodedimagebuffer.
106 * @param[in] textureId The texture id to query.defaul value is 0.
107 * @return The loading state if the texture is valid, or NOT_STARTED if the textureId
110 TextureCacheManager::LoadState GetTextureState(const TextureCacheManager::TextureId& textureId);
113 * @brief Get the current state of a texture
114 * @note This API doesn't consider external & encodedimagebuffer.
115 * @param[in] textureId The texture id to query
116 * @return The loading state if the texture is valid, or NOT_STARTED if the textureId
119 TextureCacheManager::LoadState GetTextureStateInternal(const TextureCacheManager::TextureId& textureId);
122 * @brief Get the associated texture set if the texture id is valid
123 * @param[in] textureId The texture Id to look up
124 * @param[in] textureIndex The texture index to query
125 * @return the associated texture, or an empty handle if textureId is not valid
127 Texture GetTexture(const TextureCacheManager::TextureId& textureId, uint32_t textureIndex = 0);
130 * @brief Get the external texture set information if the texture id is valid
131 * @param[in] textureId The texture Id to look up
132 * @return the external texture information. Assert if textureId is not valid
134 TextureCacheManager::ExternalTextureInfo& GetExternalTextureInfo(const TextureCacheManager::TextureId& textureId);
137 * @brief Get the encoded image buffer
138 * @param[in] bufferId The bufferId to look up
139 * @return the encoded image buffer, or an empty handle if bufferId is not valid
141 EncodedImageBuffer GetEncodedImageBuffer(const TextureCacheManager::TextureId& bufferId);
144 * @brief Get the encoded image buffer by VisualUrl
145 * @param[in] url The url to look up
146 * @return the encoded image buffer, or an empty handle if url is not buffer resource or buffer is not valid
148 EncodedImageBuffer GetEncodedImageBuffer(const VisualUrl& url);
151 * Adds an external texture to the texture manager
152 * @param[in] texture The texture to add
153 * @param[in] preMultiplied Whether this external texture preMultiplied or not. Default as false.
154 * @return string containing the URL for the texture
156 std::string AddExternalTexture(const TextureSet& texture, bool preMultiplied);
159 * Adds an encoded image buffer to the texture manager
160 * @param[in] encodedImageBuffer The image buffer to add
161 * @return string containing the URL for the texture
163 std::string AddEncodedImageBuffer(const EncodedImageBuffer& encodedImageBuffer);
166 * Removes an external texture from texture manager
167 * @param[in] url The string containing the texture to remove
168 * @return handle to the texture
170 TextureSet RemoveExternalTexture(const VisualUrl& url);
173 * Removes an external encoded image buffer from texture manager
174 * @param[in] url The string containing the encoded image buffer to remove
175 * @return handle to the encoded image buffer
177 EncodedImageBuffer RemoveEncodedImageBuffer(const VisualUrl& url);
180 * @brief Notify that external textures or encoded image buffers are used.
181 * @param[in] url The URL of the texture to use.
183 void UseExternalResource(const VisualUrl& url);
186 // To Generate / Get / Remove TextureId.
189 * @brief Generates a new valid TextureId.
190 * @param[in] textureCacheIndex the index of the cache container. If we don't setup this value, default is INVALID_CACHE_INDEX
191 * @return A TextureId
193 TextureCacheManager::TextureId GenerateTextureId(const TextureCacheIndex& textureCacheIndex = INVALID_CACHE_INDEX);
196 * @brief Used to lookup an index into the TextureInfoContainer from a TextureId
197 * @param[in] textureId The TextureId to look up
198 * @return The cache index
200 TextureCacheManager::TextureCacheIndex GetCacheIndexFromId(const TextureCacheManager::TextureId& textureId);
203 * @brief Generates a hash for caching based on the input parameters.
204 * Only applies size, fitting mode andsampling mode if the size is specified.
205 * Only applies maskTextureId if it isn't INVALID_TEXTURE_ID
206 * Always applies useAtlas.
207 * @param[in] url The URL of the image to load
208 * @param[in] size The image size
209 * @param[in] fittingMode The FittingMode to use
210 * @param[in] samplingMode The SamplingMode to use
211 * @param[in] useAtlas True if atlased
212 * @param[in] maskTextureId The masking texture id (or INVALID_TEXTURE_ID)
213 * @param[in] cropToMask True if crop to mask
214 * @param[in] frameIndex The frame index to use
215 * @return A hash of the provided data for caching.
217 TextureCacheManager::TextureHash GenerateHash(
218 const VisualUrl& url,
219 const Dali::ImageDimensions& size,
220 const Dali::FittingMode::Type& fittingMode,
221 const Dali::SamplingMode::Type& samplingMode,
222 const TextureCacheManager::UseAtlas& useAtlas,
223 const TextureCacheManager::TextureId& maskTextureId,
224 const bool& cropToMask,
225 const std::uint32_t& frameIndex);
228 * @brief Looks up a cached texture by its hash.
229 * If found, the given parameters are used to check there is no hash-collision.
230 * @param[in] hash The hash to look up
231 * @param[in] url The URL of the image to load
232 * @param[in] size The image size
233 * @param[in] fittingMode The FittingMode to use
234 * @param[in] samplingMode The SamplingMode to use
235 * @param[in] useAtlas True if atlased
236 * @param[in] storageType Whether the pixel data is stored in the cache or uploaded to the GPU
237 * @param[in] maskTextureId Optional texture ID to use to mask this image
238 * @param[in] cropToMask True if crop to mask
239 * @param[in] preMultiplyOnLoad if the image's color should be multiplied by it's alpha. Set to OFF if there is no alpha.
240 * @param[in] isAnimatedImage True if the texture is from animated image.
241 * @param[in] frameIndex The frame index to use
242 * @return A TextureCacheIndex of a cached Texture if found. Or INVALID_CACHE_INDEX if not found.
244 TextureCacheManager::TextureCacheIndex FindCachedTexture(
245 const TextureCacheManager::TextureHash& hash,
246 const VisualUrl& url,
247 const Dali::ImageDimensions& size,
248 const Dali::FittingMode::Type& fittingMode,
249 const Dali::SamplingMode::Type& samplingMode,
250 const TextureCacheManager::UseAtlas& useAtlas,
251 const StorageType& storageType,
252 const TextureCacheManager::TextureId& maskTextureId,
253 const bool& cropToMask,
254 const TextureCacheManager::MultiplyOnLoad& preMultiplyOnLoad,
255 const bool& isAnimatedImage,
256 const std::uint32_t& frameIndex);
259 * @brief Append a Texture to the TextureCacheManager.
260 * @note This API doesn't check duplication of TextureId.
261 * @note This API doesn't consider external & encodedimagebuffer.
263 * @param[in] textureInfo TextureInfo that want to cache in container.
264 * @return Index of newly appended texture info.
266 TextureCacheManager::TextureCacheIndex AppendCache(const TextureCacheManager::TextureInfo& textureInfo);
269 * @brief Remove a Texture from the TextureCacheManager.
270 * @note This API doesn't consider external & encodedimagebuffer.
272 * Textures are cached and therefore only the removal of the last
273 * occurrence of a Texture will cause its removal internally.
275 * @param[in] textureInfo TextureInfo that want to cache in container.
277 void RemoveCache(TextureCacheManager::TextureInfo& textureInfo);
281 * @brief Get TextureInfo as TextureCacheIndex.
282 * @note This API doesn't consider external & encodedimagebuffer.
283 * @param[in] textureCacheIndex Index of cahced texture.
284 * @return TextureInfo as textureCacheIndex
286 inline TextureCacheManager::TextureInfo& operator[](const TextureCacheManager::TextureCacheIndex& textureCacheIndex) noexcept
288 return mTextureInfoContainer[textureCacheIndex.GetIndex()];
292 * @brief The number of associated cached image
293 * @note This API doesn't consider external & encodedimagebuffer.
294 * @return The number of associated cached image
296 inline std::size_t size() noexcept
298 return mTextureInfoContainer.size();
302 // Private defined structs.
305 * @brief This struct is used to manage the life-cycle of EncodedImageBuffer url.
307 struct EncodedImageBufferInfo
309 EncodedImageBufferInfo(const TextureCacheManager::TextureId& bufferId,
310 const TextureCacheManager::TextureHash& bufferHash,
311 const EncodedImageBuffer& encodedImageBuffer)
312 : bufferId(bufferId),
313 bufferHash(bufferHash),
314 encodedImageBuffer(encodedImageBuffer),
318 TextureCacheManager::TextureId bufferId;
319 TextureCacheManager::TextureHash bufferHash;
320 EncodedImageBuffer encodedImageBuffer;
321 std::int16_t referenceCount;
324 typedef Dali::FreeList TextureIdConverterType; ///< The converter type from TextureId to index of TextureInfoContainer.
326 typedef std::unordered_map<TextureCacheManager::TextureHash, std::vector<TextureCacheManager::TextureId>> TextureHashContainerType; ///< The container type used to fast-find the TextureId by TextureHash.
327 typedef std::vector<TextureCacheManager::TextureInfo> TextureInfoContainerType; ///< The container type used to manage the life-cycle and caching of Textures
328 typedef std::vector<TextureCacheManager::ExternalTextureInfo> ExternalTextureInfoContainerType; ///< The container type used to manage the life-cycle and caching of ExternalTexture url
329 typedef std::vector<TextureCacheManager::EncodedImageBufferInfo> EncodedImageBufferInfoContainerType; ///< The container type used to manage the life-cycle and caching of EncodedImageBuffer url
332 // Private API: only used internally
335 * @brief Used to lookup an index into the ExternalTextureInfoContainer from a textureId
336 * @param[in] textureId The TextureId to look up
337 * @return The cache index
339 TextureCacheManager::TextureCacheIndex GetCacheIndexFromExternalTextureId(const TextureCacheManager::TextureId& textureId);
342 * @brief Used to lookup an index into the EncodedImageBufferInfoContainer from a bufferId
343 * @param[in] bufferId The bufferId to look up
344 * @return The cache index
346 TextureCacheManager::TextureCacheIndex GetCacheIndexFromEncodedImageBufferId(const TextureCacheManager::TextureId& bufferId);
349 * @brief Looks up a cached encoded image buffer cached by its hash.
350 * If found, the given parameters are used to check there is no hash-collision.
351 * @param[in] hash The hash to look up
352 * @param[in] encodedImageBuffer The image buffer to load
353 * @return A TextureCacheIndex of a cached Texture if found. Or INVALID_CACHE_INDEX if not found.
355 TextureCacheManager::TextureCacheIndex FindCachedEncodedImageBuffer(const TextureCacheManager::TextureHash& hash, const EncodedImageBuffer& encodedImageBuffer);
358 * @brief Remove id in HashContainer.
359 * @param[in] hash The hash of the texture/buffer to be delete
360 * @param[in] id The texture/buffer id to be deleted.
362 void RemoveHashId(const TextureCacheManager::TextureHash& hash, const TextureCacheManager::TextureId& id);
365 * @brief Remove data from container by the TextureCacheIndex.
366 * It also valiate the TextureIdConverter internally.
367 * We will assume that only valid TextureCacheIndex will come.
369 * @tparam ContainerType The type of container. It will automatically defined
370 * @param[in] cacheContainer The container that will remove texture info.
371 * @param[in] removeContainerIndex The index of texture info that will remove.
373 template<class ContainerType>
374 void RemoveTextureInfoByIndex(ContainerType& cacheContainer, const TextureCacheManager::TextureCacheIndex& removeContainerIndex);
378 * Deleted copy constructor.
380 TextureCacheManager(const TextureCacheManager&) = delete;
383 * Deleted assignment operator.
385 TextureCacheManager& operator=(const TextureCacheManager& rhs) = delete;
387 private: // Member Variables:
388 TextureIdConverterType mTextureIdConverter{}; ///< Convert TextureId into various container's index.
389 TextureHashContainerType mTextureHashContainer{}; ///< Used to manage the life-cycle and caching of Textures + EncodedImageBuffer by TextureHash
391 TextureInfoContainerType mTextureInfoContainer{}; ///< Used to manage the life-cycle and caching of Textures
392 ExternalTextureInfoContainerType mExternalTextures{}; ///< Externally provided textures
393 EncodedImageBufferInfoContainerType mEncodedImageBuffers{}; ///< Externally encoded image buffer
396 } // namespace Internal
398 } // namespace Toolkit
402 #endif // DALI_TOOLKIT_TEXTURE_CACHE_MANAGER_H