2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/texture-manager/texture-cache-manager.h>
22 #include <dali/devel-api/common/hash.h>
23 #include <dali/integration-api/debug.h>
34 extern Debug::Filter* gTextureManagerLogFilter; ///< Define at texture-manager-impl.cpp
37 #define GET_LOAD_STATE_STRING(loadState) \
38 loadState == TextureManagerType::LoadState::NOT_STARTED ? "NOT_STARTED" : \
39 loadState == TextureManagerType::LoadState::LOADING ? "LOADING" : \
40 loadState == TextureManagerType::LoadState::LOAD_FINISHED ? "LOAD_FINISHED" : \
41 loadState == TextureManagerType::LoadState::WAITING_FOR_MASK ? "WAITING_FOR_MASK" : \
42 loadState == TextureManagerType::LoadState::MASK_APPLYING ? "MASK_APPLYING" : \
43 loadState == TextureManagerType::LoadState::MASK_APPLIED ? "MASK_APPLIED" : \
44 loadState == TextureManagerType::LoadState::UPLOADED ? "UPLOADED" : \
45 loadState == TextureManagerType::LoadState::CANCELLED ? "CANCELLED" : \
46 loadState == TextureManagerType::LoadState::MASK_CANCELLED ? "MASK_CANCELLED" : \
47 loadState == TextureManagerType::LoadState::LOAD_FAILED ? "LOAD_FAILED" : \
52 // Due to the compile issue, this specialized template code must be defined top of this code.
54 void TextureCacheManager::RemoveTextureInfoByIndex<TextureCacheManager::EncodedImageBufferInfoContainerType>(TextureCacheManager::EncodedImageBufferInfoContainerType& cacheContainer, const TextureCacheManager::TextureCacheIndex& removeContainerIndex)
56 // Swap last data of cacheContainer.
57 if(static_cast<std::size_t>(removeContainerIndex.GetIndex() + 1) < cacheContainer.size())
59 // First, change the cache index infomations inside of converter
60 mTextureIdConverter[cacheContainer.back().bufferId] = static_cast<std::uint32_t>(removeContainerIndex);
62 // After change converter, swap the value between current data and last data.
63 std::swap(cacheContainer[removeContainerIndex.GetIndex()], cacheContainer.back());
66 // Now we can assume that latest data should be removed. pop_back.
67 cacheContainer.pop_back();
70 template<class ContainerType>
71 void TextureCacheManager::RemoveTextureInfoByIndex(ContainerType& cacheContainer, const TextureCacheManager::TextureCacheIndex& removeContainerIndex)
73 // Swap last data of cacheContainer.
74 if(static_cast<std::size_t>(removeContainerIndex.GetIndex() + 1) < cacheContainer.size())
76 // First, change the cache index infomations inside of converter
77 mTextureIdConverter[cacheContainer.back().textureId] = static_cast<std::uint32_t>(removeContainerIndex);
79 // After change converter, swap the value between current data and last data.
80 std::swap(cacheContainer[removeContainerIndex.GetIndex()], cacheContainer.back());
83 // Now we can assume that latest data should be removed. pop_back.
84 cacheContainer.pop_back();
87 TextureCacheManager::TextureCacheManager()
91 TextureCacheManager::~TextureCacheManager()
95 VisualUrl TextureCacheManager::GetVisualUrl(const TextureCacheManager::TextureId& textureId)
97 VisualUrl visualUrl("");
98 TextureCacheIndex cacheIndex = static_cast<TextureCacheIndex>(mTextureIdConverter[static_cast<std::uint32_t>(textureId)]);
100 switch(static_cast<TextureCacheIndexType>(cacheIndex.detailValue.type))
102 case TextureCacheIndexType::TEXTURE_CACHE_INDEX_TYPE_LOCAL:
104 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureCacheManager::GetVisualUrl. Using cached texture index=%d, textureId=%d\n", cacheIndex.GetIndex(), textureId);
106 TextureInfo& cachedTextureInfo(mTextureInfoContainer[cacheIndex.GetIndex()]);
107 visualUrl = cachedTextureInfo.url;
110 case TextureCacheIndexType::TEXTURE_CACHE_INDEX_TYPE_TEXTURE:
112 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureCacheManager::GetVisualUrl. Using cached external texture index=%d, textureId=%d\n", cacheIndex.GetIndex(), textureId);
113 visualUrl = VisualUrl::CreateTextureUrl(std::to_string(textureId));
116 case TextureCacheIndexType::TEXTURE_CACHE_INDEX_TYPE_BUFFER:
118 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureCacheManager::GetVisualUrl. Using cached buffer index=%d, bufferId=%d\n", cacheIndex.GetIndex(), textureId);
119 visualUrl = VisualUrl::CreateBufferUrl(std::to_string(textureId));
131 TextureCacheManager::LoadState TextureCacheManager::GetTextureState(const TextureCacheManager::TextureId& textureId)
133 LoadState loadState = TextureCacheManager::LoadState::NOT_STARTED;
134 TextureCacheIndex cacheIndex = static_cast<TextureCacheIndex>(mTextureIdConverter[static_cast<std::uint32_t>(textureId)]);
136 switch(static_cast<TextureCacheIndexType>(cacheIndex.detailValue.type))
138 case TextureCacheIndexType::TEXTURE_CACHE_INDEX_TYPE_LOCAL:
140 TextureInfo& cachedTextureInfo(mTextureInfoContainer[cacheIndex.GetIndex()]);
141 loadState = cachedTextureInfo.loadState;
144 case TextureCacheIndexType::TEXTURE_CACHE_INDEX_TYPE_TEXTURE:
146 loadState = LoadState::UPLOADED;
158 TextureCacheManager::LoadState TextureCacheManager::GetTextureStateInternal(const TextureCacheManager::TextureId& textureId)
160 LoadState loadState = TextureCacheManager::LoadState::NOT_STARTED;
161 TextureCacheIndex cacheIndex = GetCacheIndexFromId(textureId);
162 if(cacheIndex != INVALID_CACHE_INDEX)
164 TextureInfo& cachedTextureInfo(mTextureInfoContainer[cacheIndex.GetIndex()]);
165 loadState = cachedTextureInfo.loadState;
171 Texture TextureCacheManager::GetTexture(const TextureCacheManager::TextureId& textureId, uint32_t textureIndex)
173 Texture texture; // empty handle
174 TextureCacheIndex cacheIndex = GetCacheIndexFromId(textureId);
176 switch(static_cast<TextureCacheIndexType>(cacheIndex.detailValue.type))
178 case TextureCacheIndexType::TEXTURE_CACHE_INDEX_TYPE_LOCAL:
180 TextureInfo& cachedTextureInfo(mTextureInfoContainer[cacheIndex.GetIndex()]);
181 if(textureIndex < static_cast<uint32_t>(cachedTextureInfo.textures.size()))
183 texture = cachedTextureInfo.textures[textureIndex];
196 TextureCacheManager::ExternalTextureInfo& TextureCacheManager::GetExternalTextureInfo(const TextureCacheManager::TextureId& textureId)
198 TextureCacheIndex cacheIndex = GetCacheIndexFromExternalTextureId(textureId);
199 DALI_ASSERT_ALWAYS(cacheIndex != INVALID_CACHE_INDEX);
201 return mExternalTextures[cacheIndex.GetIndex()];
204 EncodedImageBuffer TextureCacheManager::GetEncodedImageBuffer(const TextureCacheManager::TextureId& bufferId)
206 EncodedImageBuffer encodedImageBuffer; // empty handle
207 TextureCacheIndex cacheIndex = GetCacheIndexFromEncodedImageBufferId(bufferId);
208 if(cacheIndex != INVALID_CACHE_INDEX)
210 encodedImageBuffer = mEncodedImageBuffers[cacheIndex.GetIndex()].encodedImageBuffer;
212 return encodedImageBuffer;
215 EncodedImageBuffer TextureCacheManager::GetEncodedImageBuffer(const VisualUrl& url)
217 EncodedImageBuffer encodedImageBuffer; // empty handle
218 if(url.IsValid() && VisualUrl::BUFFER == url.GetProtocolType())
220 std::string location = url.GetLocation();
221 if(location.size() > 0u)
223 TextureId bufferId = std::stoi(location);
224 return GetEncodedImageBuffer(bufferId);
227 return encodedImageBuffer;
230 std::string TextureCacheManager::AddExternalTexture(const TextureSet& textureSet, bool preMultiplied)
232 TextureId textureId = GenerateTextureId(TextureCacheIndex(TextureCacheIndexType::TEXTURE_CACHE_INDEX_TYPE_TEXTURE, mExternalTextures.size()));
234 TextureCacheManager::ExternalTextureInfo textureInfo(textureId, textureSet, preMultiplied);
235 mExternalTextures.emplace_back(textureInfo);
237 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureCacheManager::AddExternalTexture() : New texture registered. textureId:%d, preMultiplied:%d\n", textureInfo.textureId, preMultiplied);
239 return VisualUrl::CreateTextureUrl(std::to_string(textureInfo.textureId));
242 std::string TextureCacheManager::AddEncodedImageBuffer(const EncodedImageBuffer& encodedImageBuffer)
245 TextureHash bufferHash = static_cast<TextureHash>(encodedImageBuffer.GetHash());
246 TextureCacheIndex bufferCacheIndex = FindCachedEncodedImageBuffer(bufferHash, encodedImageBuffer);
247 if(bufferCacheIndex != INVALID_CACHE_INDEX)
249 EncodedImageBufferInfo& bufferInfo(mEncodedImageBuffers[bufferCacheIndex.GetIndex()]);
251 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureCacheManager::AddExternalEncodedImageBuffer() : Increase reference. bufferId:%d, cache index:%d, reference:%d\n", bufferInfo.bufferId, bufferCacheIndex.GetIndex(), static_cast<int>(bufferInfo.referenceCount));
253 // If same buffer added, increase reference count and return.
254 bufferInfo.referenceCount++;
255 return VisualUrl::CreateBufferUrl(std::to_string(bufferInfo.bufferId));
258 TextureId bufferId = GenerateTextureId(TextureCacheIndex(TextureCacheIndexType::TEXTURE_CACHE_INDEX_TYPE_BUFFER, mEncodedImageBuffers.size()));
260 TextureCacheManager::EncodedImageBufferInfo info(bufferId, bufferHash, encodedImageBuffer);
261 mEncodedImageBuffers.emplace_back(info);
263 // Insert TextureHashContainer
264 // Find exist list -or- Create new list.
265 std::vector<TextureId>& idList = mTextureHashContainer[bufferHash];
266 // We already assume that list doesn't contain id. just emplace back
267 idList.emplace_back(bufferId);
269 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureCacheManager::AddExternalEncodedImageBuffer() : New buffer regested. bufferId:%d\n", info.bufferId);
271 return VisualUrl::CreateBufferUrl(std::to_string(info.bufferId));
274 TextureSet TextureCacheManager::RemoveExternalTexture(const VisualUrl& url)
276 TextureSet textureSet;
277 bool removeTextureInfo = false;
278 TextureCacheIndex removeTextureIndex = INVALID_CACHE_INDEX;
281 if(VisualUrl::TEXTURE == url.GetProtocolType())
283 // get the location from the Url
284 std::string location = url.GetLocation();
285 if(location.size() > 0u)
287 TextureId textureId = std::stoi(location);
288 removeTextureIndex = GetCacheIndexFromExternalTextureId(textureId);
289 if(removeTextureIndex != INVALID_CACHE_INDEX)
291 ExternalTextureInfo& textureInfo(mExternalTextures[removeTextureIndex.GetIndex()]);
292 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureCacheManager::RemoveExternalTexture(url:%s) textureId:%d reference:%d\n", url.GetUrl().c_str(), textureId, static_cast<int>(textureInfo.referenceCount));
293 textureSet = textureInfo.textureSet;
294 if(--(textureInfo.referenceCount) <= 0)
296 removeTextureInfo = true;
297 // id life is finished. Remove it at converter
298 mTextureIdConverter.Remove(textureId);
305 // Post removal process to avoid mExternalTextures reference problems.
306 if(removeTextureInfo)
308 // Swap last data of mExternalTextures, and pop_back.
309 RemoveTextureInfoByIndex(mExternalTextures, removeTextureIndex);
314 EncodedImageBuffer TextureCacheManager::RemoveEncodedImageBuffer(const VisualUrl& url)
316 EncodedImageBuffer encodedImageBuffer;
317 bool removeBufferInfo = false;
318 TextureCacheIndex removeBufferIndex = INVALID_CACHE_INDEX;
321 if(VisualUrl::BUFFER == url.GetProtocolType())
323 // get the location from the Url
324 std::string location = url.GetLocation();
325 if(location.size() > 0u)
327 TextureId bufferId = std::stoi(location);
328 removeBufferIndex = GetCacheIndexFromEncodedImageBufferId(bufferId);
330 if(removeBufferIndex != INVALID_CACHE_INDEX)
332 EncodedImageBufferInfo& bufferInfo(mEncodedImageBuffers[removeBufferIndex.GetIndex()]);
333 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureCacheManager::RemoveEncodedImageBuffer(url:%s) bufferId:%d reference:%d\n", url.GetUrl().c_str(), bufferId, static_cast<int>(bufferInfo.referenceCount));
335 encodedImageBuffer = bufferInfo.encodedImageBuffer;
336 if(--(bufferInfo.referenceCount) <= 0)
338 removeBufferInfo = true;
339 // Step 1. remove current textureId information in mTextureHashContainer.
340 RemoveHashId(bufferInfo.bufferHash, bufferId);
341 // Step 2. id life is finished. Remove it at converter
342 mTextureIdConverter.Remove(bufferId);
349 // Post removal process to avoid mEncodedImageBuffers reference problems.
352 // Step 3. swap last data of mEncodedImageBuffers, and pop_back.
353 RemoveTextureInfoByIndex(mEncodedImageBuffers, removeBufferIndex);
355 return encodedImageBuffer;
358 void TextureCacheManager::UseExternalResource(const VisualUrl& url)
360 if(VisualUrl::TEXTURE == url.GetProtocolType())
362 std::string location = url.GetLocation();
363 if(location.size() > 0u)
365 TextureId id = std::stoi(location);
366 TextureCacheIndex cacheIndex = GetCacheIndexFromExternalTextureId(id);
367 if(cacheIndex != INVALID_CACHE_INDEX)
369 ExternalTextureInfo& textureInfo(mExternalTextures[cacheIndex.GetIndex()]);
370 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureCacheManager::UseExternalResource(url:%s) type:TEXTURE, location:%s, reference:%d\n", url.GetUrl().c_str(), url.GetLocation().c_str(), static_cast<int>(textureInfo.referenceCount));
372 textureInfo.referenceCount++;
377 else if(VisualUrl::BUFFER == url.GetProtocolType())
379 std::string location = url.GetLocation();
380 if(location.size() > 0u)
382 TextureId id = std::stoi(location);
383 TextureCacheIndex cacheIndex = GetCacheIndexFromEncodedImageBufferId(id);
384 if(cacheIndex != INVALID_CACHE_INDEX)
386 EncodedImageBufferInfo& bufferInfo(mEncodedImageBuffers[cacheIndex.GetIndex()]);
387 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureCacheManager::UseExternalResource(url:%s) type:BUFFER, location:%s, reference:%d\n", url.GetUrl().c_str(), url.GetLocation().c_str(), static_cast<int>(bufferInfo.referenceCount));
389 bufferInfo.referenceCount++;
396 TextureCacheManager::TextureId TextureCacheManager::GenerateTextureId(const TextureCacheIndex& textureCacheIndex)
398 return mTextureIdConverter.Add(static_cast<std::uint32_t>(textureCacheIndex.indexValue));
401 TextureCacheManager::TextureCacheIndex TextureCacheManager::GetCacheIndexFromId(const TextureCacheManager::TextureId& textureId)
403 if(textureId == INVALID_TEXTURE_ID) return INVALID_CACHE_INDEX;
405 TextureCacheIndex cacheIndex = static_cast<TextureCacheIndex>(mTextureIdConverter[static_cast<std::uint32_t>(textureId)]);
406 if(DALI_UNLIKELY(cacheIndex.detailValue.type != TextureCacheIndexType::TEXTURE_CACHE_INDEX_TYPE_LOCAL))
408 return INVALID_CACHE_INDEX;
411 DALI_ASSERT_DEBUG(static_cast<std::size_t>(cacheIndex.GetIndex()) < mTextureInfoContainer.size());
416 TextureCacheManager::TextureCacheIndex TextureCacheManager::GetCacheIndexFromExternalTextureId(const TextureCacheManager::TextureId& textureId)
418 if(textureId == INVALID_TEXTURE_ID) return INVALID_CACHE_INDEX;
420 TextureCacheIndex cacheIndex = static_cast<TextureCacheIndex>(mTextureIdConverter[static_cast<std::uint32_t>(textureId)]);
421 if(DALI_UNLIKELY(cacheIndex.detailValue.type != TextureCacheIndexType::TEXTURE_CACHE_INDEX_TYPE_TEXTURE))
423 return INVALID_CACHE_INDEX;
426 DALI_ASSERT_DEBUG(static_cast<std::size_t>(cacheIndex.GetIndex()) < mExternalTextures.size());
431 TextureCacheManager::TextureCacheIndex TextureCacheManager::GetCacheIndexFromEncodedImageBufferId(const TextureCacheManager::TextureId& bufferId)
433 if(bufferId == INVALID_TEXTURE_ID) return INVALID_CACHE_INDEX;
435 TextureCacheIndex cacheIndex = static_cast<TextureCacheIndex>(mTextureIdConverter[static_cast<std::uint32_t>(bufferId)]);
436 if(DALI_UNLIKELY(cacheIndex.detailValue.type != TextureCacheIndexType::TEXTURE_CACHE_INDEX_TYPE_BUFFER))
438 return INVALID_CACHE_INDEX;
441 DALI_ASSERT_DEBUG(static_cast<std::size_t>(cacheIndex.GetIndex()) < mEncodedImageBuffers.size());
446 TextureCacheManager::TextureCacheIndex TextureCacheManager::FindCachedEncodedImageBuffer(const TextureCacheManager::TextureHash& hash, const EncodedImageBuffer& encodedImageBuffer)
448 // Iterate through our hashes to find a match.
449 const auto& hashIterator = mTextureHashContainer.find(hash);
450 if(hashIterator != mTextureHashContainer.cend())
452 for(const auto& id : hashIterator->second)
454 // We have a match, now we check all the original parameters in case of a hash collision.
455 TextureCacheIndex cacheIndex = GetCacheIndexFromEncodedImageBufferId(id);
456 if(cacheIndex != INVALID_CACHE_INDEX)
458 EncodedImageBufferInfo& bufferInfo(mEncodedImageBuffers[cacheIndex.GetIndex()]);
460 if(bufferInfo.encodedImageBuffer == encodedImageBuffer)
462 // The found encoded image buffer.
469 // Default to an invalid ID, in case we do not find a match.
470 return INVALID_CACHE_INDEX;
473 TextureCacheManager::TextureHash TextureCacheManager::GenerateHash(
474 const VisualUrl& url,
475 const Dali::ImageDimensions& size,
476 const Dali::FittingMode::Type& fittingMode,
477 const Dali::SamplingMode::Type& samplingMode,
478 const TextureCacheManager::UseAtlas& useAtlas,
479 const TextureCacheManager::TextureId& maskTextureId,
480 const bool& cropToMask,
481 const std::uint32_t& frameIndex)
483 std::vector<std::uint8_t> hashTarget;
484 const uint16_t width = size.GetWidth();
485 const uint16_t height = size.GetWidth();
487 // If either the width or height has been specified, include the resizing options in the hash
488 if(width != 0 || height != 0)
490 // We are appending 5 bytes to the URL to form the hash input.
491 hashTarget.resize(5u);
492 std::uint8_t* hashTargetPtr = &(hashTarget[0u]);
494 // Pack the width and height (4 bytes total).
495 *hashTargetPtr++ = size.GetWidth() & 0xff;
496 *hashTargetPtr++ = (size.GetWidth() >> 8u) & 0xff;
497 *hashTargetPtr++ = size.GetHeight() & 0xff;
498 *hashTargetPtr++ = (size.GetHeight() >> 8u) & 0xff;
500 // Bit-pack the FittingMode, SamplingMode and atlasing.
501 // FittingMode=2bits, SamplingMode=3bits, useAtlas=1bit
502 *hashTargetPtr = (fittingMode << 4u) | (samplingMode << 1) | (useAtlas == UseAtlas::USE_ATLAS ? 1 : 0);
506 // We are not including sizing information, but we still need an extra byte for atlasing.
507 hashTarget.resize(1u);
509 // Add the atlasing to the hash input.
512 case UseAtlas::NO_ATLAS:
514 hashTarget[0u] = 'f';
517 case UseAtlas::USE_ATLAS:
519 hashTarget[0u] = 't';
525 if(maskTextureId != INVALID_TEXTURE_ID)
527 auto textureIdIndex = hashTarget.size();
528 hashTarget.resize(hashTarget.size() + sizeof(TextureId) + 1u);
529 std::uint8_t* hashTargetPtr = reinterpret_cast<std::uint8_t*>(&(hashTarget[textureIdIndex]));
531 // Append the texture id to the end of the URL byte by byte:
532 // (to avoid SIGBUS / alignment issues)
533 TextureId saltedMaskTextureId = maskTextureId;
534 for(size_t byteIter = 0; byteIter < sizeof(TextureId); ++byteIter)
536 *hashTargetPtr++ = saltedMaskTextureId & 0xff;
537 saltedMaskTextureId >>= 8u;
539 *hashTargetPtr++ = (cropToMask ? 'C' : 'M');
542 // Append the frameIndex. We don't do additional job when frameIndex = 0u due to the non-animated image case.
545 auto textureIdIndex = hashTarget.size();
546 hashTarget.resize(hashTarget.size() + sizeof(std::uint32_t));
547 std::uint8_t* hashTargetPtr = reinterpret_cast<std::uint8_t*>(&(hashTarget[textureIdIndex]));
549 // Append the frame index to the end of the URL byte by byte:
550 std::uint32_t saltedFrameIndex = frameIndex;
551 for(size_t byteIter = 0; byteIter < sizeof(std::uint8_t); ++byteIter)
553 *hashTargetPtr++ = saltedFrameIndex & 0xff;
554 saltedFrameIndex >>= 8u;
558 return url.GetUrlHash() ^ Dali::CalculateHash(hashTarget);
561 TextureCacheManager::TextureCacheIndex TextureCacheManager::FindCachedTexture(
562 const TextureCacheManager::TextureHash& hash,
563 const VisualUrl& url,
564 const Dali::ImageDimensions& size,
565 const Dali::FittingMode::Type& fittingMode,
566 const Dali::SamplingMode::Type& samplingMode,
567 const TextureCacheManager::UseAtlas& useAtlas,
568 const StorageType& storageType,
569 const TextureCacheManager::TextureId& maskTextureId,
570 const bool& cropToMask,
571 const TextureCacheManager::MultiplyOnLoad& preMultiplyOnLoad,
572 const bool& isAnimatedImage,
573 const std::uint32_t& frameIndex)
575 // Iterate through our hashes to find a match.
576 const auto& hashIterator = mTextureHashContainer.find(hash);
577 if(hashIterator != mTextureHashContainer.cend())
579 for(const auto& textureId : hashIterator->second)
581 // We have a match, now we check all the original parameters in case of a hash collision.
582 TextureCacheIndex cacheIndex = GetCacheIndexFromId(textureId);
583 if(cacheIndex != INVALID_CACHE_INDEX)
585 TextureInfo& textureInfo(mTextureInfoContainer[cacheIndex.GetIndex()]);
587 if((url.GetUrl() == textureInfo.url.GetUrl()) &&
588 (useAtlas == textureInfo.useAtlas) &&
589 (maskTextureId == textureInfo.maskTextureId) &&
590 (cropToMask == textureInfo.cropToMask) &&
591 (size == textureInfo.desiredSize) &&
592 (isAnimatedImage == textureInfo.isAnimatedImageFormat) &&
593 (storageType == textureInfo.storageType) &&
594 (frameIndex == textureInfo.frameIndex) &&
595 ((size.GetWidth() == 0 && size.GetHeight() == 0) ||
596 (fittingMode == textureInfo.fittingMode &&
597 samplingMode == textureInfo.samplingMode)))
599 // 1. If preMultiplyOnLoad is MULTIPLY_ON_LOAD, then textureInfo.preMultiplyOnLoad should be true. The premultiplication result can be different.
600 // 2. If preMultiplyOnLoad is LOAD_WITHOUT_MULTIPLY, then textureInfo.preMultiplied should be false.
601 if((preMultiplyOnLoad == MultiplyOnLoad::MULTIPLY_ON_LOAD && textureInfo.preMultiplyOnLoad) || (preMultiplyOnLoad == MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY && !textureInfo.preMultiplied))
603 // The found Texture is a match.
611 // Default to an invalid ID, in case we do not find a match.
612 return INVALID_CACHE_INDEX;
615 TextureCacheManager::TextureCacheIndex TextureCacheManager::AppendCache(const TextureCacheManager::TextureInfo& textureInfo)
617 // If we use EncodedImageBuffer, increase reference during it contains mTextureInfoContainer.
618 // This reference will be decreased when we call RemoveCache
619 if(textureInfo.url.GetProtocolType() == VisualUrl::BUFFER)
621 UseExternalResource(textureInfo.url);
624 TextureHash hash = textureInfo.hash;
625 TextureId id = textureInfo.textureId;
627 // Insert TextureHash container first
628 // Find exist list -or- Create new list.
629 std::vector<TextureId>& idList = mTextureHashContainer[hash];
630 // We already assume that list doesn't contain id. just emplace back
631 idList.emplace_back(id);
633 // Insert TextureInfo back of mTextureInfoContainer.
634 TextureCacheIndex cacheIndex = TextureCacheIndex(TextureCacheIndexType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, mTextureInfoContainer.size());
635 mTextureInfoContainer.emplace_back(textureInfo);
637 // Add converter id --> cacheIndex
638 // NOTE : We should assume that id already generated by GenerateTextureId function.
639 mTextureIdConverter[id] = cacheIndex;
644 void TextureCacheManager::RemoveCache(TextureCacheManager::TextureInfo& textureInfo)
646 TextureCacheIndex textureInfoIndex = GetCacheIndexFromId(textureInfo.textureId);
647 bool removeTextureInfo = false;
649 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureCacheManager::Remove(textureId:%d) url:%s\n cacheIdx:%d loadState:%s reference count = %d\n", textureInfo.textureId, textureInfo.url.GetUrl().c_str(), textureInfoIndex.GetIndex(), GET_LOAD_STATE_STRING(textureInfo.loadState), textureInfo.referenceCount);
651 // Decrement the reference count and check if this is the last user of this Texture.
652 if(--textureInfo.referenceCount <= 0)
654 // This is the last remove for this Texture.
655 textureInfo.referenceCount = 0;
657 // If loaded, we can remove the TextureInfo and the Atlas (if atlased).
658 if(textureInfo.loadState == LoadState::UPLOADED)
660 if(textureInfo.atlas)
662 textureInfo.atlas.Remove(textureInfo.atlasRect);
664 removeTextureInfo = true;
666 else if(textureInfo.loadState == LoadState::LOADING)
668 // We mark the textureInfo for removal.
669 // Once the load has completed, this method will be called again.
670 textureInfo.loadState = LoadState::CANCELLED;
672 else if(textureInfo.loadState == LoadState::MASK_APPLYING)
674 // We mark the textureInfo for removal.
675 // Once the load has completed, this method will be called again.
676 textureInfo.loadState = LoadState::MASK_CANCELLED;
680 // In other states, we are not waiting for a load so we are safe to remove the TextureInfo data.
681 removeTextureInfo = true;
684 // If the state allows us to remove the TextureInfo data, we do so.
685 if(removeTextureInfo)
687 // If url location is BUFFER, decrease reference count of EncodedImageBuffer.
688 if(textureInfo.url.IsBufferResource())
690 RemoveEncodedImageBuffer(textureInfo.url.GetUrl());
693 // Permanently remove the textureInfo struct.
695 // Step 1. remove current textureId information in mTextureHashContainer.
696 RemoveHashId(textureInfo.hash, textureInfo.textureId);
697 // Step 2. make textureId is not using anymore. After this job, we can reuse textureId.
698 mTextureIdConverter.Remove(textureInfo.textureId);
702 // Post removal process to avoid mTextureInfoContainer reference problems.
703 if(removeTextureInfo)
705 // Step 3. swap last data of TextureInfoContainer, and pop_back.
706 RemoveTextureInfoByIndex(mTextureInfoContainer, textureInfoIndex);
710 void TextureCacheManager::RemoveHashId(const TextureCacheManager::TextureHash& textureHash, const TextureCacheManager::TextureId& textureId)
712 auto hashIterator = mTextureHashContainer.find(textureHash);
713 if(hashIterator != mTextureHashContainer.end())
715 auto hashIdList = hashIterator->second;
716 const auto& hashIdIterator = std::find(hashIdList.cbegin(), hashIdList.cend(), textureId);
717 if(hashIdIterator != hashIdList.cend())
719 hashIdList.erase(hashIdIterator);
720 if(hashIdList.size() == 0)
722 // If id list in current hash is empty, remove it self in the container.
723 mTextureHashContainer.erase(hashIterator);
729 } // namespace Internal
731 } // namespace Toolkit