2 * Copyright (c) 2021 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/visuals/texture-manager-impl.h>
22 #include <dali/devel-api/adaptor-framework/environment-variable.h>
23 #include <dali/devel-api/adaptor-framework/image-loading.h>
24 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
25 #include <dali/devel-api/common/hash.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/public-api/math/vector4.h>
28 #include <dali/public-api/rendering/geometry.h>
33 #include <dali-toolkit/internal/image-loader/async-image-loader-impl.h>
34 #include <dali-toolkit/internal/image-loader/image-atlas-impl.h>
35 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
36 #include <dali-toolkit/internal/visuals/rendering-addon.h>
37 #include <dali-toolkit/public-api/image-loader/sync-image-loader.h>
41 constexpr auto INITIAL_CACHE_NUMBER = size_t{0u};
42 constexpr auto DEFAULT_NUMBER_OF_LOCAL_LOADER_THREADS = size_t{4u};
43 constexpr auto DEFAULT_NUMBER_OF_REMOTE_LOADER_THREADS = size_t{8u};
45 constexpr auto NUMBER_OF_LOCAL_LOADER_THREADS_ENV = "DALI_TEXTURE_LOCAL_THREADS";
46 constexpr auto NUMBER_OF_REMOTE_LOADER_THREADS_ENV = "DALI_TEXTURE_REMOTE_THREADS";
48 size_t GetNumberOfThreads(const char* environmentVariable, size_t defaultValue)
50 using Dali::EnvironmentVariable::GetEnvironmentVariable;
51 auto numberString = GetEnvironmentVariable(environmentVariable);
52 auto numberOfThreads = numberString ? std::strtoul(numberString, nullptr, 10) : 0;
53 constexpr auto MAX_NUMBER_OF_THREADS = 100u;
54 DALI_ASSERT_DEBUG(numberOfThreads < MAX_NUMBER_OF_THREADS);
55 return (numberOfThreads > 0 && numberOfThreads < MAX_NUMBER_OF_THREADS) ? numberOfThreads : defaultValue;
58 size_t GetNumberOfLocalLoaderThreads()
60 return GetNumberOfThreads(NUMBER_OF_LOCAL_LOADER_THREADS_ENV, DEFAULT_NUMBER_OF_LOCAL_LOADER_THREADS);
63 size_t GetNumberOfRemoteLoaderThreads()
65 return GetNumberOfThreads(NUMBER_OF_REMOTE_LOADER_THREADS_ENV, DEFAULT_NUMBER_OF_REMOTE_LOADER_THREADS);
79 Debug::Filter* gTextureManagerLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TEXTURE_MANAGER");
81 #define GET_LOAD_STATE_STRING(loadState) \
82 loadState == TextureManager::LoadState::NOT_STARTED ? "NOT_STARTED" : loadState == TextureManager::LoadState::LOADING ? "LOADING" \
83 : loadState == TextureManager::LoadState::LOAD_FINISHED ? "LOAD_FINISHED" \
84 : loadState == TextureManager::LoadState::WAITING_FOR_MASK ? "WAITING_FOR_MASK" \
85 : loadState == TextureManager::LoadState::MASK_APPLYING ? "MASK_APPLYING" \
86 : loadState == TextureManager::LoadState::MASK_APPLIED ? "MASK_APPLIED" \
87 : loadState == TextureManager::LoadState::UPLOADED ? "UPLOADED" \
88 : loadState == TextureManager::LoadState::CANCELLED ? "CANCELLED" \
89 : loadState == TextureManager::LoadState::LOAD_FAILED ? "LOAD_FAILED" \
94 const uint32_t DEFAULT_ATLAS_SIZE(1024u); ///< This size can fit 8 by 8 images of average size 128 * 128
95 const Vector4 FULL_ATLAS_RECT(0.0f, 0.0f, 1.0f, 1.0f); ///< UV Rectangle that covers the full Texture
96 const int INVALID_INDEX(-1); ///< Invalid index used to represent a non-existant TextureInfo struct
97 const int INVALID_CACHE_INDEX(-1); ///< Invalid Cache index
99 void PreMultiply(Devel::PixelBuffer pixelBuffer, TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
101 if(Pixel::HasAlpha(pixelBuffer.GetPixelFormat()))
103 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
105 pixelBuffer.MultiplyColorByAlpha();
110 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
114 } // Anonymous namespace
116 TextureManager::MaskingData::MaskingData()
118 mAlphaMaskId(INVALID_TEXTURE_ID),
119 mContentScaleFactor(1.0f),
124 TextureManager::TextureManager()
125 : mAsyncLocalLoaders(GetNumberOfLocalLoaderThreads(), [&]() { return AsyncLoadingHelper(*this); }),
126 mAsyncRemoteLoaders(GetNumberOfRemoteLoaderThreads(), [&]() { return AsyncLoadingHelper(*this); }),
128 mLifecycleObservers(),
130 mCurrentTextureId(0),
131 mQueueLoadFlag(false)
133 // Initialize the AddOn
134 RenderingAddOn::Get();
137 TextureManager::~TextureManager()
139 for(auto iter = mLifecycleObservers.Begin(), endIter = mLifecycleObservers.End(); iter != endIter; ++iter)
141 (*iter)->TextureManagerDestroyed();
145 TextureSet TextureManager::LoadAnimatedImageTexture(
146 Dali::AnimatedImageLoading animatedImageLoading, uint32_t frameIndex, Dali::SamplingMode::Type samplingMode, bool synchronousLoading, TextureManager::TextureId& textureId, Dali::WrapMode::Type wrapModeU, Dali::WrapMode::Type wrapModeV, TextureUploadObserver* textureObserver)
148 TextureSet textureSet;
150 if(synchronousLoading)
152 Devel::PixelBuffer pixelBuffer;
153 if(animatedImageLoading)
155 pixelBuffer = animatedImageLoading.LoadFrame(frameIndex);
159 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous loading is failed\n");
163 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
166 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight());
167 texture.Upload(pixelData);
168 textureSet = TextureSet::New();
169 textureSet.SetTexture(0u, texture);
175 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
176 textureId = RequestLoadInternal(animatedImageLoading.GetUrl(), INVALID_TEXTURE_ID, 1.0f, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, TextureManager::NO_ATLAS, false, StorageType::UPLOAD_TO_TEXTURE, textureObserver, true, TextureManager::ReloadPolicy::CACHED, preMultiply, animatedImageLoading, frameIndex);
177 TextureManager::LoadState loadState = GetTextureStateInternal(textureId);
178 if(loadState == TextureManager::LoadState::UPLOADED)
180 // UploadComplete has already been called - keep the same texture set
181 textureSet = GetTextureSet(textureId);
187 Sampler sampler = Sampler::New();
188 sampler.SetWrapMode(wrapModeU, wrapModeV);
189 textureSet.SetSampler(0u, sampler);
195 Devel::PixelBuffer TextureManager::LoadPixelBuffer(
196 const VisualUrl& url, Dali::ImageDimensions desiredSize, Dali::FittingMode::Type fittingMode, Dali::SamplingMode::Type samplingMode, bool synchronousLoading, TextureUploadObserver* textureObserver, bool orientationCorrection, TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
198 Devel::PixelBuffer pixelBuffer;
199 if(synchronousLoading)
203 if(url.IsBufferResource())
205 const EncodedImageBuffer& encodedImageBuffer = GetEncodedImageBuffer(url.GetUrl());
206 if(encodedImageBuffer)
208 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
213 pixelBuffer = LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
215 if(pixelBuffer && preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
217 PreMultiply(pixelBuffer, preMultiplyOnLoad);
223 RequestLoadInternal(url, INVALID_TEXTURE_ID, 1.0f, desiredSize, fittingMode, samplingMode, TextureManager::NO_ATLAS, false, StorageType::RETURN_PIXEL_BUFFER, textureObserver, orientationCorrection, TextureManager::ReloadPolicy::FORCED, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u);
229 TextureSet TextureManager::LoadTexture(
230 const VisualUrl& url, Dali::ImageDimensions desiredSize, Dali::FittingMode::Type fittingMode, Dali::SamplingMode::Type samplingMode, MaskingDataPointer& maskInfo, bool synchronousLoading, TextureManager::TextureId& textureId, Vector4& textureRect, Dali::ImageDimensions& textureRectSize, bool& atlasingStatus, bool& loadingStatus, Dali::WrapMode::Type wrapModeU, Dali::WrapMode::Type wrapModeV, TextureUploadObserver* textureObserver, AtlasUploadObserver* atlasObserver, ImageAtlasManagerPtr imageAtlasManager, bool orientationCorrection, TextureManager::ReloadPolicy reloadPolicy, TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
232 TextureSet textureSet;
234 loadingStatus = false;
235 textureRect = FULL_ATLAS_RECT;
237 if(VisualUrl::TEXTURE == url.GetProtocolType())
239 std::string location = url.GetLocation();
240 if(location.size() > 0u)
242 TextureId id = std::stoi(location);
243 for(auto&& elem : mExternalTextures)
245 if(elem.textureId == id)
247 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
248 textureId = elem.textureId;
249 return elem.textureSet;
254 else if(synchronousLoading)
259 Devel::PixelBuffer pixelBuffer;
260 if(url.IsBufferResource())
262 const EncodedImageBuffer& encodedImageBuffer = GetEncodedImageBuffer(url.GetUrl());
263 if(encodedImageBuffer)
265 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
270 pixelBuffer = LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
272 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
274 Devel::PixelBuffer maskPixelBuffer = LoadImageFromFile(maskInfo->mAlphaMaskUrl.GetUrl(), ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true);
277 pixelBuffer.ApplyMask(maskPixelBuffer, maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
282 PreMultiply(pixelBuffer, preMultiplyOnLoad);
283 data = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
288 DALI_LOG_ERROR("TextureManager::LoadTexture: Synchronous loading is failed\n");
292 if(atlasingStatus) // attempt atlasing
294 textureSet = imageAtlasManager->Add(textureRect, data);
296 if(!textureSet) // big image, no atlasing or atlasing failed
298 atlasingStatus = false;
299 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, data.GetPixelFormat(), data.GetWidth(), data.GetHeight());
300 texture.Upload(data);
301 textureSet = TextureSet::New();
302 textureSet.SetTexture(0u, texture);
306 textureRectSize.SetWidth(data.GetWidth());
307 textureRectSize.SetHeight(data.GetHeight());
313 loadingStatus = true;
316 textureSet = imageAtlasManager->Add(textureRect, url.GetUrl(), desiredSize, fittingMode, true, atlasObserver);
318 if(!textureSet) // big image, no atlasing or atlasing failed
320 atlasingStatus = false;
321 if(!maskInfo || !maskInfo->mAlphaMaskUrl.IsValid())
323 textureId = RequestLoad(url, desiredSize, fittingMode, samplingMode, TextureManager::NO_ATLAS, textureObserver, orientationCorrection, reloadPolicy, preMultiplyOnLoad);
327 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl);
328 textureId = RequestLoad(url,
329 maskInfo->mAlphaMaskId,
330 maskInfo->mContentScaleFactor,
334 TextureManager::NO_ATLAS,
335 maskInfo->mCropToMask,
337 orientationCorrection,
342 TextureManager::LoadState loadState = GetTextureStateInternal(textureId);
343 if(loadState == TextureManager::LoadState::UPLOADED)
345 // UploadComplete has already been called - keep the same texture set
346 textureSet = GetTextureSet(textureId);
349 // If we are loading the texture, or waiting for the ready signal handler to complete, inform
350 // caller that they need to wait.
351 loadingStatus = (loadState == TextureManager::LoadState::LOADING ||
352 loadState == TextureManager::LoadState::WAITING_FOR_MASK ||
353 loadState == TextureManager::LoadState::MASK_APPLYING ||
354 loadState == TextureManager::LoadState::MASK_APPLIED ||
355 loadState == TextureManager::LoadState::NOT_STARTED ||
360 textureRectSize = desiredSize;
364 if(!atlasingStatus && textureSet)
366 Sampler sampler = Sampler::New();
367 sampler.SetWrapMode(wrapModeU, wrapModeV);
368 textureSet.SetSampler(0u, sampler);
374 TextureManager::TextureId TextureManager::RequestLoad(
375 const VisualUrl& url,
376 const ImageDimensions desiredSize,
377 FittingMode::Type fittingMode,
378 Dali::SamplingMode::Type samplingMode,
379 const UseAtlas useAtlas,
380 TextureUploadObserver* observer,
381 bool orientationCorrection,
382 TextureManager::ReloadPolicy reloadPolicy,
383 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
385 return RequestLoadInternal(url, INVALID_TEXTURE_ID, 1.0f, desiredSize, fittingMode, samplingMode, useAtlas, false, StorageType::UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u);
388 TextureManager::TextureId TextureManager::RequestLoad(
389 const VisualUrl& url,
390 TextureId maskTextureId,
392 const ImageDimensions desiredSize,
393 FittingMode::Type fittingMode,
394 Dali::SamplingMode::Type samplingMode,
395 const UseAtlas useAtlas,
397 TextureUploadObserver* observer,
398 bool orientationCorrection,
399 TextureManager::ReloadPolicy reloadPolicy,
400 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
402 return RequestLoadInternal(url, maskTextureId, contentScale, desiredSize, fittingMode, samplingMode, useAtlas, cropToMask, StorageType::UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u);
405 TextureManager::TextureId TextureManager::RequestMaskLoad(const VisualUrl& maskUrl)
407 // Use the normal load procedure to get the alpha mask.
408 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
409 return RequestLoadInternal(maskUrl, INVALID_TEXTURE_ID, 1.0f, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, NO_ATLAS, false, StorageType::KEEP_PIXEL_BUFFER, NULL, true, TextureManager::ReloadPolicy::CACHED, preMultiply, Dali::AnimatedImageLoading(), 0u);
412 TextureManager::TextureId TextureManager::RequestLoadInternal(
413 const VisualUrl& url,
414 TextureId maskTextureId,
416 const ImageDimensions desiredSize,
417 FittingMode::Type fittingMode,
418 Dali::SamplingMode::Type samplingMode,
421 StorageType storageType,
422 TextureUploadObserver* observer,
423 bool orientationCorrection,
424 TextureManager::ReloadPolicy reloadPolicy,
425 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
426 Dali::AnimatedImageLoading animatedImageLoading,
429 // First check if the requested Texture is cached.
430 bool isAnimatedImage = (animatedImageLoading) ? true : false;
432 TextureHash textureHash = INITIAL_CACHE_NUMBER;
433 int cacheIndex = INVALID_CACHE_INDEX;
434 if(storageType != StorageType::RETURN_PIXEL_BUFFER && !isAnimatedImage)
436 textureHash = GenerateHash(url.GetUrl(), desiredSize, fittingMode, samplingMode, useAtlas, maskTextureId);
438 // Look up the texture by hash. Note: The extra parameters are used in case of a hash collision.
439 cacheIndex = FindCachedTexture(textureHash, url.GetUrl(), desiredSize, fittingMode, samplingMode, useAtlas, maskTextureId, preMultiplyOnLoad);
442 TextureManager::TextureId textureId = INVALID_TEXTURE_ID;
443 // Check if the requested Texture exists in the cache.
444 if(cacheIndex != INVALID_CACHE_INDEX)
446 if(TextureManager::ReloadPolicy::CACHED == reloadPolicy)
448 // Mark this texture being used by another client resource. Forced reload would replace the current texture
449 // without the need for incrementing the reference count.
450 ++(mTextureInfoContainer[cacheIndex].referenceCount);
452 textureId = mTextureInfoContainer[cacheIndex].textureId;
454 // Update preMultiplyOnLoad value. It should be changed according to preMultiplied value of the cached info.
455 preMultiplyOnLoad = mTextureInfoContainer[cacheIndex].preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
457 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::RequestLoad( url=%s observer=%p ) Using cached texture id@%d, textureId=%d\n", url.GetUrl().c_str(), observer, cacheIndex, textureId);
460 // Check if the requested Texture exist in Encoded Buffer
461 // This mean, that buffer is not cached, and need to be decoded.
462 if(textureId == INVALID_TEXTURE_ID && VisualUrl::BUFFER == url.GetProtocolType())
464 std::string location = url.GetLocation();
465 if(location.size() > 0u)
467 TextureId targetId = std::stoi(location);
468 const EncodedImageBuffer& encodedImageBuffer = GetEncodedImageBuffer(targetId);
469 if(encodedImageBuffer)
471 textureId = targetId;
473 // Increase EncodedImageBuffer reference during it contains mTextureInfoContainer.
474 UseExternalResource(url.GetUrl());
476 // Insert this buffer at mTextureInfoContainer.
477 // This buffer will decode at ImageLoaderThread.
478 bool preMultiply = (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
479 mTextureInfoContainer.push_back(TextureInfo(textureId, maskTextureId, url, desiredSize, contentScale, fittingMode, samplingMode, false, cropToMask, useAtlas, textureHash, orientationCorrection, preMultiply, animatedImageLoading, frameIndex));
480 cacheIndex = mTextureInfoContainer.size() - 1u;
482 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::RequestLoad( url=%s observer=%p ) New buffered texture, cacheIndex:%d, textureId=%d\n", url.GetUrl().c_str(), observer, cacheIndex, textureId);
487 if(textureId == INVALID_TEXTURE_ID) // There was no caching, or caching not required
489 // We need a new Texture.
490 textureId = GenerateUniqueTextureId();
491 bool preMultiply = (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
492 mTextureInfoContainer.push_back(TextureInfo(textureId, maskTextureId, url, desiredSize, contentScale, fittingMode, samplingMode, false, cropToMask, useAtlas, textureHash, orientationCorrection, preMultiply, animatedImageLoading, frameIndex));
493 cacheIndex = mTextureInfoContainer.size() - 1u;
495 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::RequestLoad( url=%s observer=%p ) New texture, cacheIndex:%d, textureId=%d\n", url.GetUrl().c_str(), observer, cacheIndex, textureId);
498 // The below code path is common whether we are using the cache or not.
499 // The textureInfoIndex now refers to either a pre-existing cached TextureInfo,
500 // or a new TextureInfo just created.
501 TextureInfo& textureInfo(mTextureInfoContainer[cacheIndex]);
502 textureInfo.maskTextureId = maskTextureId;
503 textureInfo.storageType = storageType;
504 textureInfo.orientationCorrection = orientationCorrection;
506 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureInfo loadState:%s\n", GET_LOAD_STATE_STRING(textureInfo.loadState));
508 // Force reloading of texture by setting loadState unless already loading or cancelled.
509 if(TextureManager::ReloadPolicy::FORCED == reloadPolicy &&
510 TextureManager::LoadState::LOADING != textureInfo.loadState &&
511 TextureManager::LoadState::WAITING_FOR_MASK != textureInfo.loadState &&
512 TextureManager::LoadState::MASK_APPLYING != textureInfo.loadState &&
513 TextureManager::LoadState::MASK_APPLIED != textureInfo.loadState &&
514 TextureManager::LoadState::CANCELLED != textureInfo.loadState)
516 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, "TextureManager::RequestLoad( url=%s observer=%p ) ForcedReload cacheIndex:%d, textureId=%d\n", url.GetUrl().c_str(), observer, cacheIndex, textureId);
518 textureInfo.loadState = TextureManager::LoadState::NOT_STARTED;
521 // Check if we should add the observer.
522 // Only do this if we have not loaded yet and it will not have loaded by the end of this method.
523 switch(textureInfo.loadState)
525 case TextureManager::LoadState::LOAD_FAILED: // Failed notifies observer which then stops observing.
526 case TextureManager::LoadState::NOT_STARTED:
528 LoadOrQueueTexture(textureInfo, observer); // If called inside NotifyObservers, queues until afterwards
531 case TextureManager::LoadState::LOADING:
532 case TextureManager::LoadState::WAITING_FOR_MASK:
533 case TextureManager::LoadState::MASK_APPLYING:
534 case TextureManager::LoadState::MASK_APPLIED:
536 ObserveTexture(textureInfo, observer);
539 case TextureManager::LoadState::UPLOADED:
543 LoadOrQueueTexture(textureInfo, observer);
547 case TextureManager::LoadState::CANCELLED:
549 // A cancelled texture hasn't finished loading yet. Treat as a loading texture
550 // (it's ref count has already been incremented, above)
551 textureInfo.loadState = TextureManager::LoadState::LOADING;
552 ObserveTexture(textureInfo, observer);
555 case TextureManager::LoadState::LOAD_FINISHED:
557 // Loading has already completed.
558 if(observer && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
560 LoadOrQueueTexture(textureInfo, observer);
566 // Return the TextureId for which this Texture can now be referenced by externally.
570 void TextureManager::Remove(const TextureManager::TextureId textureId, TextureUploadObserver* observer)
572 int textureInfoIndex = GetCacheIndexFromId(textureId);
574 if(textureInfoIndex != INVALID_INDEX)
576 TextureInfo& textureInfo(mTextureInfoContainer[textureInfoIndex]);
578 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::Remove(%d) url:%s\n cacheIdx:%d loadState:%s reference count = %d\n", textureId, textureInfo.url.GetUrl().c_str(), textureInfoIndex, GET_LOAD_STATE_STRING(textureInfo.loadState), textureInfo.referenceCount);
580 // Decrement the reference count and check if this is the last user of this Texture.
581 if(--textureInfo.referenceCount <= 0)
583 // This is the last remove for this Texture.
584 textureInfo.referenceCount = 0;
585 bool removeTextureInfo = false;
587 // If loaded, we can remove the TextureInfo and the Atlas (if atlased).
588 if(textureInfo.loadState == LoadState::UPLOADED)
590 if(textureInfo.atlas)
592 textureInfo.atlas.Remove(textureInfo.atlasRect);
594 removeTextureInfo = true;
596 else if(textureInfo.loadState == LoadState::LOADING)
598 // We mark the textureInfo for removal.
599 // Once the load has completed, this method will be called again.
600 textureInfo.loadState = LoadState::CANCELLED;
604 // In other states, we are not waiting for a load so we are safe to remove the TextureInfo data.
605 removeTextureInfo = true;
608 // If the state allows us to remove the TextureInfo data, we do so.
609 if(removeTextureInfo)
611 // If url location is BUFFER, decrease reference count of EncodedImageBuffer.
612 if(textureInfo.url.IsBufferResource())
614 RemoveExternalEncodedImageBuffer(textureInfo.url.GetUrl());
616 // Permanently remove the textureInfo struct.
617 mTextureInfoContainer.erase(mTextureInfoContainer.begin() + textureInfoIndex);
623 // Remove element from the LoadQueue
624 for(auto&& element : mLoadQueue)
626 if(element.mObserver == observer)
628 // Do not erase the item. We will clear it later in ProcessQueuedTextures().
629 element.mObserver = nullptr;
637 VisualUrl TextureManager::GetVisualUrl(TextureId textureId)
639 VisualUrl visualUrl("");
640 int cacheIndex = GetCacheIndexFromId(textureId);
642 if(cacheIndex != INVALID_CACHE_INDEX)
644 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::GetVisualUrl. Using cached texture id=%d, textureId=%d\n", cacheIndex, textureId);
646 TextureInfo& cachedTextureInfo(mTextureInfoContainer[cacheIndex]);
647 visualUrl = cachedTextureInfo.url;
652 TextureManager::LoadState TextureManager::GetTextureState(TextureId textureId)
654 LoadState loadState = TextureManager::LoadState::NOT_STARTED;
656 int cacheIndex = GetCacheIndexFromId(textureId);
657 if(cacheIndex != INVALID_CACHE_INDEX)
659 TextureInfo& cachedTextureInfo(mTextureInfoContainer[cacheIndex]);
660 loadState = cachedTextureInfo.loadState;
664 for(auto&& elem : mExternalTextures)
666 if(elem.textureId == textureId)
668 loadState = LoadState::UPLOADED;
676 TextureManager::LoadState TextureManager::GetTextureStateInternal(TextureId textureId)
678 LoadState loadState = TextureManager::LoadState::NOT_STARTED;
680 int cacheIndex = GetCacheIndexFromId(textureId);
681 if(cacheIndex != INVALID_CACHE_INDEX)
683 TextureInfo& cachedTextureInfo(mTextureInfoContainer[cacheIndex]);
684 loadState = cachedTextureInfo.loadState;
690 TextureSet TextureManager::GetTextureSet(TextureId textureId)
692 TextureSet textureSet; // empty handle
694 int cacheIndex = GetCacheIndexFromId(textureId);
695 if(cacheIndex != INVALID_CACHE_INDEX)
697 TextureInfo& cachedTextureInfo(mTextureInfoContainer[cacheIndex]);
698 textureSet = cachedTextureInfo.textureSet;
702 for(auto&& elem : mExternalTextures)
704 if(elem.textureId == textureId)
706 textureSet = elem.textureSet;
714 EncodedImageBuffer TextureManager::GetEncodedImageBuffer(TextureId textureId)
716 EncodedImageBuffer encodedImageBuffer; // empty handle
717 for(auto&& elem : mEncodedBufferTextures)
719 if(elem.textureId == textureId)
721 encodedImageBuffer = elem.encodedImageBuffer;
725 return encodedImageBuffer;
728 EncodedImageBuffer TextureManager::GetEncodedImageBuffer(const std::string& url)
730 EncodedImageBuffer encodedImageBuffer; // empty handle
731 if(url.size() > 0 && VisualUrl::BUFFER == VisualUrl::GetProtocolType(url))
733 std::string location = VisualUrl::GetLocation(url);
734 if(location.size() > 0u)
736 TextureId targetId = std::stoi(location);
737 return GetEncodedImageBuffer(targetId);
740 return encodedImageBuffer;
743 std::string TextureManager::AddExternalTexture(TextureSet& textureSet)
745 TextureManager::ExternalTextureInfo info;
746 info.textureId = GenerateUniqueTextureId();
747 info.textureSet = textureSet;
748 mExternalTextures.emplace_back(info);
750 return VisualUrl::CreateTextureUrl(std::to_string(info.textureId));
753 std::string TextureManager::AddExternalEncodedImageBuffer(const EncodedImageBuffer& encodedImageBuffer)
756 for(auto&& elem : mEncodedBufferTextures)
758 if(elem.encodedImageBuffer == encodedImageBuffer)
760 // If same buffer added, increase reference count and return.
761 elem.referenceCount++;
762 return VisualUrl::CreateBufferUrl(std::to_string(elem.textureId));;
765 TextureManager::EncodedBufferTextureInfo info(GenerateUniqueTextureId(), encodedImageBuffer);
766 mEncodedBufferTextures.emplace_back(info);
767 return VisualUrl::CreateBufferUrl(std::to_string(info.textureId));
770 TextureSet TextureManager::RemoveExternalTexture(const std::string& url)
774 if(VisualUrl::TEXTURE == VisualUrl::GetProtocolType(url))
776 // get the location from the Url
777 std::string location = VisualUrl::GetLocation(url);
778 if(location.size() > 0u)
780 TextureId id = std::stoi(location);
781 const auto end = mExternalTextures.end();
782 for(auto iter = mExternalTextures.begin(); iter != end; ++iter)
784 if(iter->textureId == id)
786 auto textureSet = iter->textureSet;
787 if(--(iter->referenceCount) <= 0)
789 mExternalTextures.erase(iter);
800 EncodedImageBuffer TextureManager::RemoveExternalEncodedImageBuffer(const std::string& url)
804 if(VisualUrl::BUFFER == VisualUrl::GetProtocolType(url))
806 // get the location from the Url
807 std::string location = VisualUrl::GetLocation(url);
808 if(location.size() > 0u)
810 TextureId id = std::stoi(location);
811 const auto end = mEncodedBufferTextures.end();
812 for(auto iter = mEncodedBufferTextures.begin(); iter != end; ++iter)
814 if(iter->textureId == id)
816 auto encodedImageBuffer = iter->encodedImageBuffer;
817 if(--(iter->referenceCount) <= 0)
819 mEncodedBufferTextures.erase(iter);
821 return encodedImageBuffer;
827 return EncodedImageBuffer();
830 void TextureManager::UseExternalResource(const VisualUrl& url)
832 if(VisualUrl::TEXTURE == url.GetProtocolType())
834 std::string location = url.GetLocation();
835 if(location.size() > 0u)
837 TextureId id = std::stoi(location);
838 for(auto&& elem : mExternalTextures)
840 if(elem.textureId == id)
842 elem.referenceCount++;
848 else if(VisualUrl::BUFFER == url.GetProtocolType())
850 std::string location = url.GetLocation();
851 if(location.size() > 0u)
853 TextureId id = std::stoi(location);
854 for(auto&& elem : mEncodedBufferTextures)
856 if(elem.textureId == id)
858 elem.referenceCount++;
866 void TextureManager::AddObserver(TextureManager::LifecycleObserver& observer)
868 // make sure an observer doesn't observe the same object twice
869 // otherwise it will get multiple calls to ObjectDestroyed()
870 DALI_ASSERT_DEBUG(mLifecycleObservers.End() == std::find(mLifecycleObservers.Begin(), mLifecycleObservers.End(), &observer));
871 mLifecycleObservers.PushBack(&observer);
874 void TextureManager::RemoveObserver(TextureManager::LifecycleObserver& observer)
876 // Find the observer...
877 auto endIter = mLifecycleObservers.End();
878 for(auto iter = mLifecycleObservers.Begin(); iter != endIter; ++iter)
880 if((*iter) == &observer)
882 mLifecycleObservers.Erase(iter);
886 DALI_ASSERT_DEBUG(endIter != mLifecycleObservers.End());
889 void TextureManager::LoadOrQueueTexture(TextureInfo& textureInfo, TextureUploadObserver* observer)
891 switch(textureInfo.loadState)
893 case LoadState::NOT_STARTED:
894 case LoadState::LOAD_FAILED:
898 QueueLoadTexture(textureInfo, observer);
902 LoadTexture(textureInfo, observer);
906 case LoadState::UPLOADED:
910 QueueLoadTexture(textureInfo, observer);
914 // The Texture has already loaded. The other observers have already been notified.
915 // We need to send a "late" loaded notification for this observer.
916 observer->UploadComplete(true, textureInfo.textureId, textureInfo.textureSet, textureInfo.useAtlas, textureInfo.atlasRect, textureInfo.preMultiplied);
920 case LoadState::LOADING:
921 case LoadState::CANCELLED:
922 case LoadState::LOAD_FINISHED:
923 case LoadState::WAITING_FOR_MASK:
924 case LoadState::MASK_APPLYING:
925 case LoadState::MASK_APPLIED:
932 void TextureManager::QueueLoadTexture(TextureInfo& textureInfo, TextureUploadObserver* observer)
934 auto textureId = textureInfo.textureId;
935 mLoadQueue.PushBack(LoadQueueElement(textureId, observer));
937 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
940 void TextureManager::LoadTexture(TextureInfo& textureInfo, TextureUploadObserver* observer)
942 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::LoadTexture(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
944 textureInfo.loadState = LoadState::LOADING;
945 if(!textureInfo.loadSynchronously)
947 auto& loadersContainer = (textureInfo.url.IsLocalResource() || textureInfo.url.IsBufferResource()) ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
948 auto loadingHelperIt = loadersContainer.GetNext();
949 auto premultiplyOnLoad = (textureInfo.preMultiplyOnLoad && textureInfo.maskTextureId == INVALID_TEXTURE_ID) ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
950 DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
951 if(textureInfo.animatedImageLoading)
953 loadingHelperIt->LoadAnimatedImage(textureInfo.textureId, textureInfo.animatedImageLoading, textureInfo.frameIndex);
957 loadingHelperIt->Load(textureInfo.textureId, textureInfo.url, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, textureInfo.orientationCorrection, premultiplyOnLoad);
960 ObserveTexture(textureInfo, observer);
963 void TextureManager::ProcessQueuedTextures()
965 for(auto&& element : mLoadQueue)
967 if(!element.mObserver)
972 int cacheIndex = GetCacheIndexFromId(element.mTextureId);
973 if(cacheIndex != INVALID_CACHE_INDEX)
975 TextureInfo& textureInfo(mTextureInfoContainer[cacheIndex]);
976 if(textureInfo.loadState == LoadState::UPLOADED)
978 element.mObserver->UploadComplete(true, textureInfo.textureId, textureInfo.textureSet, textureInfo.useAtlas, textureInfo.atlasRect, textureInfo.preMultiplied);
980 else if(textureInfo.loadState == LoadState::LOAD_FINISHED && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
982 element.mObserver->LoadComplete(true, textureInfo.pixelBuffer, textureInfo.url, textureInfo.preMultiplied);
986 LoadTexture(textureInfo, element.mObserver);
993 void TextureManager::ObserveTexture(TextureInfo& textureInfo,
994 TextureUploadObserver* observer)
996 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ObserveTexture(): url:%s observer:%p\n", textureInfo.url.GetUrl().c_str(), observer);
1000 textureInfo.observerList.PushBack(observer);
1001 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
1005 void TextureManager::AsyncLoadComplete(AsyncLoadingInfoContainerType& loadingContainer, uint32_t id, Devel::PixelBuffer pixelBuffer)
1007 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::AsyncLoadComplete( id:%d )\n", id);
1009 if(loadingContainer.size() >= 1u)
1011 AsyncLoadingInfo loadingInfo = loadingContainer.front();
1013 if(loadingInfo.loadId == id)
1015 int cacheIndex = GetCacheIndexFromId(loadingInfo.textureId);
1016 if(cacheIndex != INVALID_CACHE_INDEX)
1018 TextureInfo& textureInfo(mTextureInfoContainer[cacheIndex]);
1020 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, " textureId:%d Url:%s CacheIndex:%d LoadState: %d\n", textureInfo.textureId, textureInfo.url.GetUrl().c_str(), cacheIndex, textureInfo.loadState);
1022 if(textureInfo.loadState != LoadState::CANCELLED)
1024 // textureInfo can be invalidated after this call (as the mTextureInfoContainer may be modified)
1025 PostLoad(textureInfo, pixelBuffer);
1029 Remove(textureInfo.textureId, nullptr);
1034 loadingContainer.pop_front();
1038 void TextureManager::PostLoad(TextureInfo& textureInfo, Devel::PixelBuffer& pixelBuffer)
1040 // Was the load successful?
1041 if(pixelBuffer && (pixelBuffer.GetWidth() != 0) && (pixelBuffer.GetHeight() != 0))
1043 // No atlas support for now
1044 textureInfo.useAtlas = NO_ATLAS;
1045 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
1047 if(textureInfo.storageType == StorageType::UPLOAD_TO_TEXTURE)
1049 // If there is a mask texture ID associated with this texture, then apply the mask
1050 // if it's already loaded. If it hasn't, and the mask is still loading,
1051 // wait for the mask to finish loading.
1052 if(textureInfo.maskTextureId != INVALID_TEXTURE_ID)
1054 if(textureInfo.loadState == LoadState::MASK_APPLYING)
1056 textureInfo.loadState = LoadState::MASK_APPLIED;
1057 UploadTexture(pixelBuffer, textureInfo);
1058 NotifyObservers(textureInfo, true);
1062 LoadState maskLoadState = GetTextureStateInternal(textureInfo.maskTextureId);
1063 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
1064 if(maskLoadState == LoadState::LOADING)
1066 textureInfo.loadState = LoadState::WAITING_FOR_MASK;
1068 else if(maskLoadState == LoadState::LOAD_FINISHED)
1070 // Send New Task to Thread
1071 ApplyMask(textureInfo, textureInfo.maskTextureId);
1077 UploadTexture(pixelBuffer, textureInfo);
1078 NotifyObservers(textureInfo, true);
1083 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
1084 textureInfo.loadState = LoadState::LOAD_FINISHED;
1086 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1088 NotifyObservers(textureInfo, true);
1092 // Check if there was another texture waiting for this load to complete
1093 // (e.g. if this was an image mask, and its load is on a different thread)
1094 CheckForWaitingTexture(textureInfo);
1100 textureInfo.loadState = LoadState::LOAD_FAILED;
1101 CheckForWaitingTexture(textureInfo);
1102 NotifyObservers(textureInfo, false);
1106 void TextureManager::CheckForWaitingTexture(TextureInfo& maskTextureInfo)
1108 // Search the cache, checking if any texture has this texture id as a
1110 const unsigned int size = mTextureInfoContainer.size();
1112 for(unsigned int cacheIndex = 0; cacheIndex < size; ++cacheIndex)
1114 if(mTextureInfoContainer[cacheIndex].maskTextureId == maskTextureInfo.textureId &&
1115 mTextureInfoContainer[cacheIndex].loadState == LoadState::WAITING_FOR_MASK)
1117 TextureInfo& textureInfo(mTextureInfoContainer[cacheIndex]);
1119 if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED)
1121 // Send New Task to Thread
1122 ApplyMask(textureInfo, maskTextureInfo.textureId);
1126 textureInfo.pixelBuffer.Reset();
1127 textureInfo.loadState = LoadState::LOAD_FAILED;
1128 NotifyObservers(textureInfo, false);
1134 void TextureManager::ApplyMask(TextureInfo& textureInfo, TextureId maskTextureId)
1136 int maskCacheIndex = GetCacheIndexFromId(maskTextureId);
1137 if(maskCacheIndex != INVALID_CACHE_INDEX)
1139 Devel::PixelBuffer maskPixelBuffer = mTextureInfoContainer[maskCacheIndex].pixelBuffer;
1140 Devel::PixelBuffer pixelBuffer = textureInfo.pixelBuffer;
1141 textureInfo.pixelBuffer.Reset();
1143 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ApplyMask(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
1145 textureInfo.loadState = LoadState::MASK_APPLYING;
1146 auto& loadersContainer = (textureInfo.url.IsLocalResource() || textureInfo.url.IsBufferResource()) ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
1147 auto loadingHelperIt = loadersContainer.GetNext();
1148 auto premultiplyOnLoad = textureInfo.preMultiplyOnLoad ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
1149 DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
1150 loadingHelperIt->ApplyMask(textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask, premultiplyOnLoad);
1154 void TextureManager::UploadTexture(Devel::PixelBuffer& pixelBuffer, TextureInfo& textureInfo)
1156 if(textureInfo.useAtlas != USE_ATLAS)
1158 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, " TextureManager::UploadTexture() New Texture for textureId:%d\n", textureInfo.textureId);
1160 // Check if this pixelBuffer is premultiplied
1161 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
1163 auto& renderingAddOn = RenderingAddOn::Get();
1164 if(renderingAddOn.IsValid())
1166 renderingAddOn.CreateGeometry(textureInfo.textureId, pixelBuffer);
1169 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight());
1171 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
1172 texture.Upload(pixelData);
1173 if(!textureInfo.textureSet)
1175 textureInfo.textureSet = TextureSet::New();
1177 textureInfo.textureSet.SetTexture(0u, texture);
1180 // Update the load state.
1181 // Note: This is regardless of success as we care about whether a
1182 // load attempt is in progress or not. If unsuccessful, a broken
1183 // image is still loaded.
1184 textureInfo.loadState = LoadState::UPLOADED;
1187 void TextureManager::NotifyObservers(TextureInfo& textureInfo, bool success)
1189 TextureId textureId = textureInfo.textureId;
1191 // If there is an observer: Notify the load is complete, whether successful or not,
1192 // and erase it from the list
1193 TextureInfo* info = &textureInfo;
1195 mQueueLoadFlag = true;
1197 while(info->observerList.Count())
1199 TextureUploadObserver* observer = info->observerList[0];
1201 // During UploadComplete() a Control ResourceReady() signal is emitted.
1202 // During that signal the app may add remove /add Textures (e.g. via
1204 // It is possible for observers to be removed from the observer list,
1205 // and it is also possible for the mTextureInfoContainer to be modified,
1206 // invalidating the reference to the textureInfo struct.
1207 // Texture load requests for the same URL are deferred until the end of this
1209 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "NotifyObservers() url:%s loadState:%s\n", textureInfo.url.GetUrl().c_str(), GET_LOAD_STATE_STRING(textureInfo.loadState));
1211 // It is possible for the observer to be deleted.
1212 // Disconnect and remove the observer first.
1213 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1215 info->observerList.Erase(info->observerList.begin());
1217 if(info->storageType == StorageType::RETURN_PIXEL_BUFFER)
1219 observer->LoadComplete(success, info->pixelBuffer, info->url, info->preMultiplied);
1223 observer->UploadComplete(success, info->textureId, info->textureSet, info->useAtlas, info->atlasRect, info->preMultiplied);
1226 // Get the textureInfo from the container again as it may have been invalidated.
1227 int textureInfoIndex = GetCacheIndexFromId(textureId);
1228 if(textureInfoIndex == INVALID_CACHE_INDEX)
1230 break; // texture has been removed - can stop.
1232 info = &mTextureInfoContainer[textureInfoIndex];
1235 mQueueLoadFlag = false;
1236 ProcessQueuedTextures();
1238 if(info->storageType == StorageType::RETURN_PIXEL_BUFFER && info->observerList.Count() == 0)
1240 Remove(info->textureId, nullptr);
1244 TextureManager::TextureId TextureManager::GenerateUniqueTextureId()
1246 return mCurrentTextureId++;
1249 int TextureManager::GetCacheIndexFromId(const TextureId textureId)
1251 const unsigned int size = mTextureInfoContainer.size();
1253 for(unsigned int i = 0; i < size; ++i)
1255 if(mTextureInfoContainer[i].textureId == textureId)
1261 return INVALID_CACHE_INDEX;
1264 TextureManager::TextureHash TextureManager::GenerateHash(
1265 const std::string& url,
1266 const ImageDimensions size,
1267 const FittingMode::Type fittingMode,
1268 const Dali::SamplingMode::Type samplingMode,
1269 const UseAtlas useAtlas,
1270 TextureId maskTextureId)
1272 std::string hashTarget(url);
1273 const size_t urlLength = hashTarget.length();
1274 const uint16_t width = size.GetWidth();
1275 const uint16_t height = size.GetWidth();
1277 // If either the width or height has been specified, include the resizing options in the hash
1278 if(width != 0 || height != 0)
1280 // We are appending 5 bytes to the URL to form the hash input.
1281 hashTarget.resize(urlLength + 5u);
1282 char* hashTargetPtr = &(hashTarget[urlLength]);
1284 // Pack the width and height (4 bytes total).
1285 *hashTargetPtr++ = size.GetWidth() & 0xff;
1286 *hashTargetPtr++ = (size.GetWidth() >> 8u) & 0xff;
1287 *hashTargetPtr++ = size.GetHeight() & 0xff;
1288 *hashTargetPtr++ = (size.GetHeight() >> 8u) & 0xff;
1290 // Bit-pack the FittingMode, SamplingMode and atlasing.
1291 // FittingMode=2bits, SamplingMode=3bits, useAtlas=1bit
1292 *hashTargetPtr = (fittingMode << 4u) | (samplingMode << 1) | useAtlas;
1296 // We are not including sizing information, but we still need an extra byte for atlasing.
1297 hashTarget.resize(urlLength + 1u);
1299 // Add the atlasing to the hash input.
1302 case UseAtlas::NO_ATLAS:
1304 hashTarget[urlLength] = 'f';
1307 case UseAtlas::USE_ATLAS:
1309 hashTarget[urlLength] = 't';
1315 if(maskTextureId != INVALID_TEXTURE_ID)
1317 auto textureIdIndex = hashTarget.length();
1318 hashTarget.resize(hashTarget.length() + sizeof(TextureId));
1319 unsigned char* hashTargetPtr = reinterpret_cast<unsigned char*>(&(hashTarget[textureIdIndex]));
1321 // Append the texture id to the end of the URL byte by byte:
1322 // (to avoid SIGBUS / alignment issues)
1323 for(size_t byteIter = 0; byteIter < sizeof(TextureId); ++byteIter)
1325 *hashTargetPtr++ = maskTextureId & 0xff;
1326 maskTextureId >>= 8u;
1330 return Dali::CalculateHash(hashTarget);
1333 int TextureManager::FindCachedTexture(
1334 const TextureManager::TextureHash hash,
1335 const std::string& url,
1336 const ImageDimensions size,
1337 const FittingMode::Type fittingMode,
1338 const Dali::SamplingMode::Type samplingMode,
1339 const bool useAtlas,
1340 TextureId maskTextureId,
1341 TextureManager::MultiplyOnLoad preMultiplyOnLoad)
1343 // Default to an invalid ID, in case we do not find a match.
1344 int cacheIndex = INVALID_CACHE_INDEX;
1346 // Iterate through our hashes to find a match.
1347 const unsigned int count = mTextureInfoContainer.size();
1348 for(unsigned int i = 0u; i < count; ++i)
1350 if(mTextureInfoContainer[i].hash == hash)
1352 // We have a match, now we check all the original parameters in case of a hash collision.
1353 TextureInfo& textureInfo(mTextureInfoContainer[i]);
1355 if((url == textureInfo.url.GetUrl()) &&
1356 (useAtlas == textureInfo.useAtlas) &&
1357 (maskTextureId == textureInfo.maskTextureId) &&
1358 (size == textureInfo.desiredSize) &&
1359 ((size.GetWidth() == 0 && size.GetHeight() == 0) ||
1360 (fittingMode == textureInfo.fittingMode &&
1361 samplingMode == textureInfo.samplingMode)))
1363 // 1. If preMultiplyOnLoad is MULTIPLY_ON_LOAD, then textureInfo.preMultiplyOnLoad should be true. The premultiplication result can be different.
1364 // 2. If preMultiplyOnLoad is LOAD_WITHOUT_MULTIPLY, then textureInfo.preMultiplied should be false.
1365 if((preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD && textureInfo.preMultiplyOnLoad) || (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY && !textureInfo.preMultiplied))
1367 // The found Texture is a match.
1378 void TextureManager::ObserverDestroyed(TextureUploadObserver* observer)
1380 const unsigned int count = mTextureInfoContainer.size();
1381 for(unsigned int i = 0; i < count; ++i)
1383 TextureInfo& textureInfo(mTextureInfoContainer[i]);
1384 for(TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin();
1385 j != textureInfo.observerList.End();)
1389 j = textureInfo.observerList.Erase(j);
1398 // Remove element from the LoadQueue
1399 for(auto&& element : mLoadQueue)
1401 if(element.mObserver == observer)
1403 element.mObserver = nullptr;
1408 TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(TextureManager& textureManager)
1409 : AsyncLoadingHelper(Toolkit::AsyncImageLoader::New(), textureManager, AsyncLoadingInfoContainerType())
1413 void TextureManager::AsyncLoadingHelper::LoadAnimatedImage(TextureId textureId,
1414 Dali::AnimatedImageLoading animatedImageLoading,
1415 uint32_t frameIndex)
1417 mLoadingInfoContainer.push_back(AsyncLoadingInfo(textureId));
1418 auto id = GetImplementation(mLoader).LoadAnimatedImage(animatedImageLoading, frameIndex);
1419 mLoadingInfoContainer.back().loadId = id;
1422 void TextureManager::AsyncLoadingHelper::Load(TextureId textureId,
1423 const VisualUrl& url,
1424 ImageDimensions desiredSize,
1425 FittingMode::Type fittingMode,
1426 SamplingMode::Type samplingMode,
1427 bool orientationCorrection,
1428 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
1430 mLoadingInfoContainer.push_back(AsyncLoadingInfo(textureId));
1431 if(DALI_UNLIKELY(url.IsBufferResource()))
1433 auto id = GetImplementation(mLoader).LoadEncodedImageBuffer(mTextureManager.GetEncodedImageBuffer(url.GetUrl()), desiredSize, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad);
1434 mLoadingInfoContainer.back().loadId = id;
1438 auto id = GetImplementation(mLoader).Load(url, desiredSize, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad);
1439 mLoadingInfoContainer.back().loadId = id;
1443 void TextureManager::AsyncLoadingHelper::ApplyMask(TextureId textureId,
1444 Devel::PixelBuffer pixelBuffer,
1445 Devel::PixelBuffer maskPixelBuffer,
1448 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
1450 mLoadingInfoContainer.push_back(AsyncLoadingInfo(textureId));
1451 auto id = GetImplementation(mLoader).ApplyMask(pixelBuffer, maskPixelBuffer, contentScale, cropToMask, preMultiplyOnLoad);
1452 mLoadingInfoContainer.back().loadId = id;
1455 TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(AsyncLoadingHelper&& rhs)
1456 : AsyncLoadingHelper(rhs.mLoader, rhs.mTextureManager, std::move(rhs.mLoadingInfoContainer))
1460 TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(
1461 Toolkit::AsyncImageLoader loader,
1462 TextureManager& textureManager,
1463 AsyncLoadingInfoContainerType&& loadingInfoContainer)
1465 mTextureManager(textureManager),
1466 mLoadingInfoContainer(std::move(loadingInfoContainer))
1468 DevelAsyncImageLoader::PixelBufferLoadedSignal(mLoader).Connect(
1469 this, &AsyncLoadingHelper::AsyncLoadComplete);
1472 void TextureManager::AsyncLoadingHelper::AsyncLoadComplete(uint32_t id,
1473 Devel::PixelBuffer pixelBuffer)
1475 mTextureManager.AsyncLoadComplete(mLoadingInfoContainer, id, pixelBuffer);
1478 Geometry TextureManager::GetRenderGeometry(TextureId textureId, uint32_t& frontElements, uint32_t& backElements)
1480 return RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().GetGeometry(textureId, frontElements, backElements) : Geometry();
1483 } // namespace Internal
1485 } // namespace Toolkit