1 #ifndef DALI_TOOLKIT_TEXTURE_CACHE_MANAGER_H
2 #define DALI_TOOLKIT_TEXTURE_CACHE_MANAGER_H
5 * Copyright (c) 2022 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/public-api/adaptor-framework/encoded-image-buffer.h>
24 #include <dali-toolkit/internal/texture-manager/texture-manager-type.h>
25 #include <dali-toolkit/internal/texture-manager/texture-upload-observer.h>
26 #include <dali-toolkit/internal/visuals/visual-url.h>
35 * @brief The contain and managing cached textures.
36 * Each Texture hold there TextureId. These TextureId can be used outside of TextureManager.
37 * Internally, each cached texture can be accessed by TextureCacheIndex.
38 * You can Convert TextureId into TextureCacheIndex by this class.
40 * Also, You can store external TextureSet or EncodedImageBuffer here.
42 class TextureCacheManager
45 // Copy enum and types and const values that TextureCacheManager will use.
46 using TextureId = TextureManagerType::TextureId;
47 using TextureCacheIndex = TextureManagerType::TextureCacheIndex;
48 using TextureHash = TextureManagerType::TextureHash;
50 static constexpr TextureId INVALID_TEXTURE_ID = TextureManagerType::INVALID_TEXTURE_ID;
51 static constexpr TextureCacheIndex INVALID_CACHE_INDEX = TextureManagerType::INVALID_CACHE_INDEX;
53 using UseAtlas = TextureManagerType::UseAtlas;
54 using StorageType = TextureManagerType::StorageType;
55 using LoadType = TextureManagerType::LoadType;
56 using LoadState = TextureManagerType::LoadState;
57 using ReloadPolicy = TextureManagerType::ReloadPolicy;
58 using MultiplyOnLoad = TextureManagerType::MultiplyOnLoad;
59 using TextureInfo = TextureManagerType::TextureInfo;
65 TextureCacheManager();
70 ~TextureCacheManager();
73 // TextureCacheManager Main API:
76 * @brief Get the visualUrl associated with the texture id.
77 * @param[in] textureId The texture Id to get
78 * @return The visual Url associated with the texture id.
80 VisualUrl GetVisualUrl(const TextureCacheManager::TextureId& textureId);
83 * @brief Get the current state of a texture
84 * @param[in] textureId The texture id to query
85 * @return The loading state if the texture is valid, or NOT_STARTED if the textureId
88 TextureCacheManager::LoadState GetTextureState(const TextureCacheManager::TextureId& textureId);
91 * @brief Get the current state of a texture
92 * @param[in] textureId The texture id to query
93 * @return The loading state if the texture is valid, or NOT_STARTED if the textureId
96 TextureCacheManager::LoadState GetTextureStateInternal(const TextureCacheManager::TextureId& textureId);
99 * @brief Get the associated texture set if the texture id is valid
100 * @param[in] textureId The texture Id to look up
101 * @return the associated texture set, or an empty handle if textureId is not valid
103 TextureSet GetTextureSet(const TextureCacheManager::TextureId& textureId);
106 * @brief Get the external texture set if the texture id is valid
107 * @param[in] textureId The texture Id to look up
108 * @return the external texture set, or an empty handle if textureId is not valid
110 TextureSet GetExternalTextureSet(const TextureCacheManager::TextureId& textureId);
113 * @brief Get the encoded image buffer
114 * @param[in] textureId The textureId to look up
115 * @return the encoded image buffer, or an empty handle if textureId is not valid
117 EncodedImageBuffer GetEncodedImageBuffer(const TextureCacheManager::TextureId& textureId);
120 * @brief Get the encoded image buffer by VisualUrl
121 * @param[in] url The url to look up
122 * @return the encoded image buffer, or an empty handle if url is not buffer resource or buffer is not valid
124 EncodedImageBuffer GetEncodedImageBuffer(const std::string& url);
127 * Adds an external texture to the texture manager
128 * @param[in] texture The texture to add
129 * @return string containing the URL for the texture
131 std::string AddExternalTexture(const TextureSet& texture);
134 * Adds an external encoded image buffer to the texture manager
135 * @param[in] encodedImageBuffer The image buffer to add
136 * @return string containing the URL for the texture
138 std::string AddExternalEncodedImageBuffer(const EncodedImageBuffer& encodedImageBuffer);
141 * Removes an external texture from texture manager
142 * @param[in] url The string containing the texture to remove
143 * @return handle to the texture
145 TextureSet RemoveExternalTexture(const std::string& url);
148 * Removes an external encoded image buffer from texture manager
149 * @param[in] url The string containing the encoded image buffer to remove
150 * @return handle to the encoded image buffer
152 EncodedImageBuffer RemoveExternalEncodedImageBuffer(const std::string& url);
155 * @brief Notify that external textures or external encoded image buffers are used.
156 * @param[in] url The URL of the texture to use.
158 void UseExternalResource(const VisualUrl& url);
161 // To Generate / Get / Remove TextureId.
164 * @brief Generates a new, unique TextureId
165 * @return A unique TextureId
167 TextureCacheManager::TextureId GenerateUniqueTextureId();
170 * @brief Used to lookup an index into the TextureInfoContainer from a TextureId
171 * @param[in] textureId The TextureId to look up
172 * @return The cache index
174 TextureCacheManager::TextureCacheIndex GetCacheIndexFromId(const TextureCacheManager::TextureId& textureId);
177 * @brief Generates a hash for caching based on the input parameters.
178 * Only applies size, fitting mode andsampling mode if the size is specified.
179 * Only applies maskTextureId if it isn't INVALID_TEXTURE_ID
180 * Always applies useAtlas.
181 * @param[in] url The URL of the image to load
182 * @param[in] size The image size
183 * @param[in] fittingMode The FittingMode to use
184 * @param[in] samplingMode The SamplingMode to use
185 * @param[in] useAtlas True if atlased
186 * @param[in] maskTextureId The masking texture id (or INVALID_TEXTURE_ID)
187 * @return A hash of the provided data for caching.
189 TextureCacheManager::TextureHash GenerateHash(
190 const std::string& url,
191 const Dali::ImageDimensions& size,
192 const Dali::FittingMode::Type& fittingMode,
193 const Dali::SamplingMode::Type& samplingMode,
194 const TextureCacheManager::UseAtlas& useAtlas,
195 const TextureCacheManager::TextureId& maskTextureId);
198 * @brief Looks up a cached texture by its hash.
199 * If found, the given parameters are used to check there is no hash-collision.
200 * @param[in] hash The hash to look up
201 * @param[in] url The URL of the image to load
202 * @param[in] size The image size
203 * @param[in] fittingMode The FittingMode to use
204 * @param[in] samplingMode The SamplingMode to use
205 * @param[in] useAtlas True if atlased
206 * @param[in] maskTextureId Optional texture ID to use to mask this image
207 * @param[in] preMultiplyOnLoad if the image's color should be multiplied by it's alpha. Set to OFF if there is no alpha.
208 * @return A TextureCacheId of a cached Texture if found. Or INVALID_CACHE_INDEX if not found.
210 TextureCacheManager::TextureCacheIndex FindCachedTexture(
211 const TextureCacheManager::TextureHash& hash,
212 const std::string& url,
213 const Dali::ImageDimensions& size,
214 const Dali::FittingMode::Type& fittingMode,
215 const Dali::SamplingMode::Type& samplingMode,
216 const TextureCacheManager::UseAtlas& useAtlas,
217 const TextureCacheManager::TextureId& maskTextureId,
218 const TextureCacheManager::MultiplyOnLoad& preMultiplyOnLoad);
221 * @brief Append a Texture to the TextureCacheManager.
222 * @note This API doesn't check duplication of TextureId.
224 * @param[in] textureInfo TextureInfo that want to cache in container.
225 * @return Index of newly appended texture info.
227 TextureCacheManager::TextureCacheIndex AppendCache(const TextureCacheManager::TextureInfo& textureInfo);
230 * @brief Remove a Texture from the TextureCacheManager.
232 * Textures are cached and therefore only the removal of the last
233 * occurrence of a Texture will cause its removal internally.
235 * @param[in] textureId The Id of the Texture to remove at Cache.
237 void RemoveCache(const TextureCacheManager::TextureId& textureId);
241 * @brief Get TextureInfo as TextureCacheIndex.
242 * @note This API doesn't consider external & encodedimagebuffer.
243 * @param[in] textureCacheIndex Index of cahced texture.
244 * @return TextureInfo as textureCacheIndex
246 inline TextureCacheManager::TextureInfo& operator[](const TextureCacheManager::TextureCacheIndex& textureCacheIndex) noexcept
248 return mTextureInfoContainer[textureCacheIndex];
252 * @brief The number of associated cached image
253 * @note This API doesn't consider external & encodedimagebuffer.
254 * @return The number of associated cached image
256 inline std::size_t size() noexcept
258 return mTextureInfoContainer.size();
262 // Private defined structs.
264 struct ExternalTextureInfo
266 ExternalTextureInfo(const TextureCacheManager::TextureId& textureId,
267 const TextureSet& textureSet)
268 : textureId(textureId),
269 textureSet(textureSet),
273 TextureCacheManager::TextureId textureId;
274 TextureSet textureSet;
275 std::int16_t referenceCount;
278 struct EncodedBufferTextureInfo
280 EncodedBufferTextureInfo(const TextureCacheManager::TextureId& textureId,
281 const EncodedImageBuffer& encodedImageBuffer)
282 : textureId(textureId),
283 encodedImageBuffer(encodedImageBuffer),
287 TextureCacheManager::TextureId textureId;
288 EncodedImageBuffer encodedImageBuffer;
289 std::int16_t referenceCount;
292 typedef std::vector<TextureCacheManager::TextureInfo> TextureInfoContainerType; ///< The container type used to manage the life-cycle and caching of Textures
293 typedef std::vector<TextureCacheManager::ExternalTextureInfo> ExternalTextureInfoContainerType; ///< The container type used to manage the life-cycle and caching of Textures
294 typedef std::vector<TextureCacheManager::EncodedBufferTextureInfo> EncodedBufferTextureInfoContainerType; ///< The container type used to manage the life-cycle and caching of Textures
298 * Deleted copy constructor.
300 TextureCacheManager(const TextureCacheManager&) = delete;
303 * Deleted assignment operator.
305 TextureCacheManager& operator=(const TextureCacheManager& rhs) = delete;
307 private: // Member Variables:
308 TextureInfoContainerType mTextureInfoContainer{}; ///< Used to manage the life-cycle and caching of Textures
309 ExternalTextureInfoContainerType mExternalTextures{}; ///< Externally provided textures
310 EncodedBufferTextureInfoContainerType mEncodedBufferTextures{}; ///< Externally encoded buffer textures
311 TextureCacheManager::TextureId mCurrentTextureId; ///< The current value used for the unique Texture Id generation
314 } // namespace Internal
316 } // namespace Toolkit
320 #endif // DALI_TOOLKIT_TEXTURE_CACHE_MANAGER_H