1 #ifndef DALI_TOOLKIT_TEXTURE_MANAGER_TYPE_H
2 #define DALI_TOOLKIT_TEXTURE_MANAGER_TYPE_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/devel-api/adaptor-framework/animated-image-loading.h>
22 #include <dali/public-api/common/dali-vector.h>
23 #include <dali/public-api/math/vector4.h>
26 #include <dali-toolkit/devel-api/image-loader/image-atlas.h>
27 #include <dali-toolkit/internal/texture-manager/texture-upload-observer.h>
28 #include <dali-toolkit/internal/visuals/visual-url.h>
37 * @brief Define common type, enum, and struct that both TextureManager and TextureCacheManager will use.
39 namespace TextureManagerType
43 enum TextureCacheIndexType
45 TEXTURE_CACHE_INDEX_FREE_LIST = 0, ///< Only for FreeList. We should not use this for TextureCacheIndex.
46 TEXTURE_CACHE_INDEX_TYPE_LOCAL = 1,
47 TEXTURE_CACHE_INDEX_TYPE_TEXTURE,
48 TEXTURE_CACHE_INDEX_TYPE_BUFFER,
49 TEXTURE_CACHE_INDEX_TYPE_MASKING, ///< Not implemented yet.
50 TEXTURE_CACHE_INDEX_TYPE_MAX = 7, ///< Maximum number of cache type we can use.
56 * @brief standard union type of texture index.
57 * Due to the FreeList can only use for uint32_t and we need to seperate
58 * each index per container type, we can only hold maximum 2^28 textures
60 * 0 ~ 2^28 - 1 : index of FreeList. TextureCacheManager will not use it.
61 * 2^28 ~ 2*2^28 - 1 : index of mTextureInfoContainer, the main texture container.
62 * 2*2^28 ~ 3*2^28 - 1 : index of mExternalTextures.
63 * 3*2^28 ~ 4*2^28 - 1 : index of mEncodedBufferTextures.
65 union TextureCacheIndexData
67 TextureCacheIndexData() = default;
68 constexpr TextureCacheIndexData(const std::uint32_t& index)
72 constexpr explicit TextureCacheIndexData(const std::int32_t& index)
73 : indexValue(static_cast<std::uint32_t>(index))
76 constexpr TextureCacheIndexData(const TextureCacheIndexType& type, const std::uint32_t& index)
77 : detailValue{index, type}
80 constexpr TextureCacheIndexData(const TextureCacheIndexData& indexData)
81 : indexValue(indexData.indexValue)
84 constexpr TextureCacheIndexData(TextureCacheIndexData&& indexData)
85 : indexValue(indexData.indexValue)
89 TextureCacheIndexData& operator=(const std::uint32_t& index)
94 TextureCacheIndexData& operator=(const TextureCacheIndexData& rhs)
96 indexValue = rhs.indexValue;
99 TextureCacheIndexData& operator=(TextureCacheIndexData&& rhs)
101 indexValue = rhs.indexValue;
105 constexpr operator std::uint32_t() const
109 constexpr operator std::uint32_t()
113 constexpr explicit operator std::int32_t() const
115 return static_cast<std::int32_t>(indexValue);
117 constexpr explicit operator std::int32_t()
119 return static_cast<std::int32_t>(indexValue);
122 // Return detailValue.index. - the real index of datailValue.type container
123 constexpr inline std::uint32_t GetIndex() const
125 return detailValue.index;
128 inline constexpr bool operator==(const TextureCacheIndexData& rhs)
130 return indexValue == rhs.indexValue;
132 inline constexpr bool operator<(const TextureCacheIndexData& rhs)
134 return indexValue < rhs.indexValue;
138 std::uint32_t indexValue;
141 unsigned int index : 28;
142 TextureCacheIndexType type : 4;
148 typedef std::int32_t TextureId; ///< The TextureId type. This is used as a handle to refer to a particular Texture.
149 typedef TextureCacheIndexData TextureCacheIndex; ///< The TextureCacheIndex type. This is used as a handles to refer to a particular Texture in TextureCacheManager.
150 /// Note : For the same Texture, TextureId will not be changed. But TextureCacheIndex can be chaged when TextureCacheManager
151 /// Internal container informations changed by Append or Remove.
152 typedef std::size_t TextureHash; ///< The type used to store the hash used for Texture caching.
156 static constexpr TextureId INVALID_TEXTURE_ID = -1; ///< Used to represent a null TextureId or error
157 static constexpr TextureCacheIndex INVALID_CACHE_INDEX = 0; ///< Used to represent a null TextureCacheIndex or error
162 * Whether the texture should be atlased or uploaded into it's own GPU texture
171 * Whether the pixel data should be kept in TextureManager, returned with pixelBuffer or uploaded for rendering
173 enum class StorageType
175 KEEP_PIXEL_BUFFER, ///< Keep loaded pixel buffer inside of texture manager without making texture. This could be used for inside pixel process like mask image.
176 RETURN_PIXEL_BUFFER, ///< Return loaded pixel buffer without making texture.
177 /// Because a pixel buffer cannot be used multiple texture, this pixel buffer only cached during loading, and is removed after loading is finished.
178 KEEP_TEXTURE, ///< Keep loaded texture inside of texture manager. This could be used for pixel processing like GPU masking.
179 UPLOAD_TO_TEXTURE ///< Loaded image will be uploaded to texture and the texture will be returned.
183 * Whether the texture should be loaded synchronously or asynchronously.
192 * @brief The LoadState Enumeration represents the current state of a particular Texture's life-cycle.
196 NOT_STARTED, ///< Default
197 LOADING, ///< Loading has been started, but not finished.
198 LOAD_FINISHED, ///< Loading has finished. (for CPU storage only)
199 WAITING_FOR_MASK, ///< Loading has finished, but waiting for mask image
200 MASK_APPLYING, ///< Loading has finished, Mask is applying
201 MASK_APPLIED, ///< Loading has finished, Mask is applyied by GPU
202 UPLOADED, ///< Uploaded and ready. (For GPU upload only)
203 CANCELLED, ///< Removed before loading completed
204 LOAD_FAILED ///< Async loading failed, e.g. connection problem
208 * @brief Types of reloading policies
210 enum class ReloadPolicy
212 CACHED = 0, ///< Loads cached texture if it exists.
213 FORCED ///< Forces reloading of texture.
217 * @brief Whether to multiply alpha into color channels on load
219 enum class MultiplyOnLoad
221 LOAD_WITHOUT_MULTIPLY = 0, ///< Don't modify the image
222 MULTIPLY_ON_LOAD ///< Multiply alpha into color channels on load
228 * @brief This struct is used to manage the life-cycle of Texture loading and caching.
232 TextureInfo(const TextureId& textureId,
233 const TextureId& maskTextureId,
234 const VisualUrl& url,
235 const Dali::ImageDimensions& desiredSize,
236 const float& scaleFactor,
237 const Dali::FittingMode::Type& fittingMode,
238 const Dali::SamplingMode::Type& samplingMode,
239 const bool& loadSynchronously,
240 const bool& cropToMask,
241 const UseAtlas& useAtlas,
242 const TextureHash& hash,
243 const bool& orientationCorrection,
244 const bool& preMultiplyOnLoad,
245 const Dali::AnimatedImageLoading& animatedImageLoading,
246 const std::uint32_t& frameIndex,
247 const bool& loadYuvPlanes)
249 desiredSize(desiredSize),
250 useSize(desiredSize),
251 atlasRect(0.0f, 0.0f, 1.0f, 1.0f), // Full atlas rectangle
252 textureId(textureId),
253 maskTextureId(maskTextureId),
255 scaleFactor(scaleFactor),
257 loadState(LoadState::NOT_STARTED),
258 fittingMode(fittingMode),
259 samplingMode(samplingMode),
260 storageType(StorageType::UPLOAD_TO_TEXTURE),
261 animatedImageLoading(animatedImageLoading),
262 frameIndex(frameIndex),
266 loadSynchronously(loadSynchronously),
267 cropToMask(cropToMask),
268 orientationCorrection(true),
269 preMultiplyOnLoad(preMultiplyOnLoad),
270 preMultiplied(false),
271 loadYuvPlanes(loadYuvPlanes)
273 isAnimatedImageFormat = (animatedImageLoading) ? true : false;
277 * Container type used to store all observer clients of this Texture
279 typedef Dali::Vector<TextureUploadObserver*> ObserverListType;
281 ObserverListType observerList; ///< Container used to store all observer clients of this Texture
282 Dali::Toolkit::ImageAtlas atlas; ///< The atlas this Texture lays within (if any)
283 Dali::Devel::PixelBuffer pixelBuffer; ///< The PixelBuffer holding the image data (May be empty after upload)
284 std::vector<Dali::Texture> textures; ///< The Textures
285 VisualUrl url; ///< The URL of the image
286 Dali::ImageDimensions desiredSize; ///< The size requested
287 Dali::ImageDimensions useSize; ///< The size used
288 Dali::Vector4 atlasRect; ///< The atlas rect used if atlased
289 TextureId textureId; ///< The TextureId associated with this Texture
290 TextureId maskTextureId; ///< The mask TextureId to be applied on load
291 TextureHash hash; ///< The hash used to cache this Texture
292 float scaleFactor; ///< The scale factor to apply to the Texture when masking
293 std::int16_t referenceCount; ///< The reference count of clients using this Texture
294 LoadState loadState; ///< The load state showing the load progress of the Texture
295 Dali::FittingMode::Type fittingMode : 3; ///< The requested FittingMode
296 Dali::SamplingMode::Type samplingMode : 3; ///< The requested SamplingMode
297 StorageType storageType; ///< CPU storage / GPU upload;
298 Dali::AnimatedImageLoading animatedImageLoading; ///< AnimatedImageLoading that contains animated image information.
299 uint32_t frameIndex; ///< Frame index that be loaded, in case of animated image
300 uint32_t frameCount; ///< Total frame count of input animated image. If this variable is not 0, this textureInfo is for animated image file format.
301 uint32_t frameInterval; ///< Time interval between this frame and next frame of animated image.
302 UseAtlas useAtlas; ///< USE_ATLAS if an atlas was requested.
304 bool loadSynchronously : 1; ///< True if synchronous loading was requested
305 bool cropToMask : 1; ///< True if the image should be cropped to the mask size.
306 bool orientationCorrection : 1; ///< True if the image should be rotated to match exif orientation data
307 bool preMultiplyOnLoad : 1; ///< True if the image's color should be multiplied by it's alpha
308 bool preMultiplied : 1; ///< True if the image's color was multiplied by it's alpha
309 bool isAnimatedImageFormat : 1; ///< true if the image is requested from animated image visual.
310 bool loadYuvPlanes : 1; ///< true if the image should be loaded as yuv planes
313 } // namespace TextureManagerType
315 } // namespace Internal
317 } // namespace Toolkit
321 #endif // DALI_TOOLKIT_TEXTURE_MANAGER_TYPE_H