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;
472 // Insert this buffer at mTextureInfoContainer.
473 // This buffer will decode at ImageLoaderThread.
474 bool preMultiply = (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
475 mTextureInfoContainer.push_back(TextureInfo(textureId, maskTextureId, url, desiredSize, contentScale, fittingMode, samplingMode, false, cropToMask, useAtlas, textureHash, orientationCorrection, preMultiply, animatedImageLoading, frameIndex));
476 cacheIndex = mTextureInfoContainer.size() - 1u;
478 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);
483 if(textureId == INVALID_TEXTURE_ID) // There was no caching, or caching not required
485 // We need a new Texture.
486 textureId = GenerateUniqueTextureId();
487 bool preMultiply = (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
488 mTextureInfoContainer.push_back(TextureInfo(textureId, maskTextureId, url, desiredSize, contentScale, fittingMode, samplingMode, false, cropToMask, useAtlas, textureHash, orientationCorrection, preMultiply, animatedImageLoading, frameIndex));
489 cacheIndex = mTextureInfoContainer.size() - 1u;
491 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);
494 // The below code path is common whether we are using the cache or not.
495 // The textureInfoIndex now refers to either a pre-existing cached TextureInfo,
496 // or a new TextureInfo just created.
497 TextureInfo& textureInfo(mTextureInfoContainer[cacheIndex]);
498 textureInfo.maskTextureId = maskTextureId;
499 textureInfo.storageType = storageType;
500 textureInfo.orientationCorrection = orientationCorrection;
502 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureInfo loadState:%s\n", GET_LOAD_STATE_STRING(textureInfo.loadState));
504 // Force reloading of texture by setting loadState unless already loading or cancelled.
505 if(TextureManager::ReloadPolicy::FORCED == reloadPolicy &&
506 TextureManager::LoadState::LOADING != textureInfo.loadState &&
507 TextureManager::LoadState::WAITING_FOR_MASK != textureInfo.loadState &&
508 TextureManager::LoadState::MASK_APPLYING != textureInfo.loadState &&
509 TextureManager::LoadState::MASK_APPLIED != textureInfo.loadState &&
510 TextureManager::LoadState::CANCELLED != textureInfo.loadState)
512 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);
514 textureInfo.loadState = TextureManager::LoadState::NOT_STARTED;
517 // Check if we should add the observer.
518 // Only do this if we have not loaded yet and it will not have loaded by the end of this method.
519 switch(textureInfo.loadState)
521 case TextureManager::LoadState::LOAD_FAILED: // Failed notifies observer which then stops observing.
522 case TextureManager::LoadState::NOT_STARTED:
524 LoadOrQueueTexture(textureInfo, observer); // If called inside NotifyObservers, queues until afterwards
527 case TextureManager::LoadState::LOADING:
528 case TextureManager::LoadState::WAITING_FOR_MASK:
529 case TextureManager::LoadState::MASK_APPLYING:
530 case TextureManager::LoadState::MASK_APPLIED:
532 ObserveTexture(textureInfo, observer);
535 case TextureManager::LoadState::UPLOADED:
539 LoadOrQueueTexture(textureInfo, observer);
543 case TextureManager::LoadState::CANCELLED:
545 // A cancelled texture hasn't finished loading yet. Treat as a loading texture
546 // (it's ref count has already been incremented, above)
547 textureInfo.loadState = TextureManager::LoadState::LOADING;
548 ObserveTexture(textureInfo, observer);
551 case TextureManager::LoadState::LOAD_FINISHED:
553 // Loading has already completed.
554 if(observer && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
556 LoadOrQueueTexture(textureInfo, observer);
562 // Return the TextureId for which this Texture can now be referenced by externally.
566 void TextureManager::Remove(const TextureManager::TextureId textureId, TextureUploadObserver* observer)
568 int textureInfoIndex = GetCacheIndexFromId(textureId);
570 if(textureInfoIndex != INVALID_INDEX)
572 TextureInfo& textureInfo(mTextureInfoContainer[textureInfoIndex]);
574 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);
576 // Decrement the reference count and check if this is the last user of this Texture.
577 if(--textureInfo.referenceCount <= 0)
579 // This is the last remove for this Texture.
580 textureInfo.referenceCount = 0;
581 bool removeTextureInfo = false;
583 // If loaded, we can remove the TextureInfo and the Atlas (if atlased).
584 if(textureInfo.loadState == LoadState::UPLOADED)
586 if(textureInfo.atlas)
588 textureInfo.atlas.Remove(textureInfo.atlasRect);
590 removeTextureInfo = true;
592 else if(textureInfo.loadState == LoadState::LOADING)
594 // We mark the textureInfo for removal.
595 // Once the load has completed, this method will be called again.
596 textureInfo.loadState = LoadState::CANCELLED;
600 // In other states, we are not waiting for a load so we are safe to remove the TextureInfo data.
601 removeTextureInfo = true;
604 // If the state allows us to remove the TextureInfo data, we do so.
605 if(removeTextureInfo)
607 // Permanently remove the textureInfo struct.
608 mTextureInfoContainer.erase(mTextureInfoContainer.begin() + textureInfoIndex);
614 // Remove element from the LoadQueue
615 for(auto&& element : mLoadQueue)
617 if(element.mObserver == observer)
619 // Do not erase the item. We will clear it later in ProcessQueuedTextures().
620 element.mObserver = nullptr;
628 VisualUrl TextureManager::GetVisualUrl(TextureId textureId)
630 VisualUrl visualUrl("");
631 int cacheIndex = GetCacheIndexFromId(textureId);
633 if(cacheIndex != INVALID_CACHE_INDEX)
635 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::GetVisualUrl. Using cached texture id=%d, textureId=%d\n", cacheIndex, textureId);
637 TextureInfo& cachedTextureInfo(mTextureInfoContainer[cacheIndex]);
638 visualUrl = cachedTextureInfo.url;
643 TextureManager::LoadState TextureManager::GetTextureState(TextureId textureId)
645 LoadState loadState = TextureManager::LoadState::NOT_STARTED;
647 int cacheIndex = GetCacheIndexFromId(textureId);
648 if(cacheIndex != INVALID_CACHE_INDEX)
650 TextureInfo& cachedTextureInfo(mTextureInfoContainer[cacheIndex]);
651 loadState = cachedTextureInfo.loadState;
655 for(auto&& elem : mExternalTextures)
657 if(elem.textureId == textureId)
659 loadState = LoadState::UPLOADED;
667 TextureManager::LoadState TextureManager::GetTextureStateInternal(TextureId textureId)
669 LoadState loadState = TextureManager::LoadState::NOT_STARTED;
671 int cacheIndex = GetCacheIndexFromId(textureId);
672 if(cacheIndex != INVALID_CACHE_INDEX)
674 TextureInfo& cachedTextureInfo(mTextureInfoContainer[cacheIndex]);
675 loadState = cachedTextureInfo.loadState;
681 TextureSet TextureManager::GetTextureSet(TextureId textureId)
683 TextureSet textureSet; // empty handle
685 int cacheIndex = GetCacheIndexFromId(textureId);
686 if(cacheIndex != INVALID_CACHE_INDEX)
688 TextureInfo& cachedTextureInfo(mTextureInfoContainer[cacheIndex]);
689 textureSet = cachedTextureInfo.textureSet;
693 for(auto&& elem : mExternalTextures)
695 if(elem.textureId == textureId)
697 textureSet = elem.textureSet;
705 EncodedImageBuffer TextureManager::GetEncodedImageBuffer(TextureId textureId)
707 EncodedImageBuffer encodedImageBuffer; // empty handle
708 for(auto&& elem : mEncodedBufferTextures)
710 if(elem.textureId == textureId)
712 encodedImageBuffer = elem.encodedImageBuffer;
716 return encodedImageBuffer;
719 EncodedImageBuffer TextureManager::GetEncodedImageBuffer(const std::string& url)
721 EncodedImageBuffer encodedImageBuffer; // empty handle
722 if(url.size() > 0 && VisualUrl::BUFFER == VisualUrl::GetProtocolType(url))
724 std::string location = VisualUrl::GetLocation(url);
725 if(location.size() > 0u)
727 TextureId targetId = std::stoi(location);
728 return GetEncodedImageBuffer(targetId);
731 return encodedImageBuffer;
734 std::string TextureManager::AddExternalTexture(TextureSet& textureSet)
736 TextureManager::ExternalTextureInfo info;
737 info.textureId = GenerateUniqueTextureId();
738 info.textureSet = textureSet;
739 mExternalTextures.emplace_back(info);
741 return VisualUrl::CreateTextureUrl(std::to_string(info.textureId));
744 std::string TextureManager::AddExternalEncodedImageBuffer(const EncodedImageBuffer& encodedImageBuffer)
747 for(auto&& elem : mEncodedBufferTextures)
749 if(elem.encodedImageBuffer == encodedImageBuffer)
751 // If same buffer added, increase reference count and return.
752 elem.referenceCount++;
753 return VisualUrl::CreateBufferUrl(std::to_string(elem.textureId));;
756 TextureManager::EncodedBufferTextureInfo info(GenerateUniqueTextureId(), encodedImageBuffer);
757 mEncodedBufferTextures.emplace_back(info);
758 return VisualUrl::CreateBufferUrl(std::to_string(info.textureId));
761 TextureSet TextureManager::RemoveExternalTexture(const std::string& url)
765 if(VisualUrl::TEXTURE == VisualUrl::GetProtocolType(url))
767 // get the location from the Url
768 std::string location = VisualUrl::GetLocation(url);
769 if(location.size() > 0u)
771 TextureId id = std::stoi(location);
772 const auto end = mExternalTextures.end();
773 for(auto iter = mExternalTextures.begin(); iter != end; ++iter)
775 if(iter->textureId == id)
777 auto textureSet = iter->textureSet;
778 if(--(iter->referenceCount) <= 0)
780 mExternalTextures.erase(iter);
791 EncodedImageBuffer TextureManager::RemoveExternalEncodedImageBuffer(const std::string& url)
795 if(VisualUrl::BUFFER == VisualUrl::GetProtocolType(url))
797 // get the location from the Url
798 std::string location = VisualUrl::GetLocation(url);
799 if(location.size() > 0u)
801 TextureId id = std::stoi(location);
802 const auto end = mEncodedBufferTextures.end();
803 for(auto iter = mEncodedBufferTextures.begin(); iter != end; ++iter)
805 if(iter->textureId == id)
807 auto encodedImageBuffer = iter->encodedImageBuffer;
808 if(--(iter->referenceCount) <= 0)
810 mEncodedBufferTextures.erase(iter);
812 return encodedImageBuffer;
818 return EncodedImageBuffer();
821 void TextureManager::UseExternalResource(const VisualUrl& url)
823 if(VisualUrl::TEXTURE == url.GetProtocolType())
825 std::string location = url.GetLocation();
826 if(location.size() > 0u)
828 TextureId id = std::stoi(location);
829 for(auto&& elem : mExternalTextures)
831 if(elem.textureId == id)
833 elem.referenceCount++;
839 else if(VisualUrl::BUFFER == url.GetProtocolType())
841 std::string location = url.GetLocation();
842 if(location.size() > 0u)
844 TextureId id = std::stoi(location);
845 for(auto&& elem : mEncodedBufferTextures)
847 if(elem.textureId == id)
849 elem.referenceCount++;
857 void TextureManager::AddObserver(TextureManager::LifecycleObserver& observer)
859 // make sure an observer doesn't observe the same object twice
860 // otherwise it will get multiple calls to ObjectDestroyed()
861 DALI_ASSERT_DEBUG(mLifecycleObservers.End() == std::find(mLifecycleObservers.Begin(), mLifecycleObservers.End(), &observer));
862 mLifecycleObservers.PushBack(&observer);
865 void TextureManager::RemoveObserver(TextureManager::LifecycleObserver& observer)
867 // Find the observer...
868 auto endIter = mLifecycleObservers.End();
869 for(auto iter = mLifecycleObservers.Begin(); iter != endIter; ++iter)
871 if((*iter) == &observer)
873 mLifecycleObservers.Erase(iter);
877 DALI_ASSERT_DEBUG(endIter != mLifecycleObservers.End());
880 void TextureManager::LoadOrQueueTexture(TextureInfo& textureInfo, TextureUploadObserver* observer)
882 switch(textureInfo.loadState)
884 case LoadState::NOT_STARTED:
885 case LoadState::LOAD_FAILED:
889 QueueLoadTexture(textureInfo, observer);
893 LoadTexture(textureInfo, observer);
897 case LoadState::UPLOADED:
901 QueueLoadTexture(textureInfo, observer);
905 // The Texture has already loaded. The other observers have already been notified.
906 // We need to send a "late" loaded notification for this observer.
907 observer->UploadComplete(true, textureInfo.textureId, textureInfo.textureSet, textureInfo.useAtlas, textureInfo.atlasRect, textureInfo.preMultiplied);
911 case LoadState::LOADING:
912 case LoadState::CANCELLED:
913 case LoadState::LOAD_FINISHED:
914 case LoadState::WAITING_FOR_MASK:
915 case LoadState::MASK_APPLYING:
916 case LoadState::MASK_APPLIED:
923 void TextureManager::QueueLoadTexture(TextureInfo& textureInfo, TextureUploadObserver* observer)
925 auto textureId = textureInfo.textureId;
926 mLoadQueue.PushBack(LoadQueueElement(textureId, observer));
928 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
931 void TextureManager::LoadTexture(TextureInfo& textureInfo, TextureUploadObserver* observer)
933 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::LoadTexture(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
935 textureInfo.loadState = LoadState::LOADING;
936 if(!textureInfo.loadSynchronously)
938 auto& loadersContainer = (textureInfo.url.IsLocalResource() || textureInfo.url.IsBufferResource()) ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
939 auto loadingHelperIt = loadersContainer.GetNext();
940 auto premultiplyOnLoad = (textureInfo.preMultiplyOnLoad && textureInfo.maskTextureId == INVALID_TEXTURE_ID) ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
941 DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
942 if(textureInfo.animatedImageLoading)
944 loadingHelperIt->LoadAnimatedImage(textureInfo.textureId, textureInfo.animatedImageLoading, textureInfo.frameIndex);
948 loadingHelperIt->Load(textureInfo.textureId, textureInfo.url, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, textureInfo.orientationCorrection, premultiplyOnLoad);
951 ObserveTexture(textureInfo, observer);
954 void TextureManager::ProcessQueuedTextures()
956 for(auto&& element : mLoadQueue)
958 if(!element.mObserver)
963 int cacheIndex = GetCacheIndexFromId(element.mTextureId);
964 if(cacheIndex != INVALID_CACHE_INDEX)
966 TextureInfo& textureInfo(mTextureInfoContainer[cacheIndex]);
967 if(textureInfo.loadState == LoadState::UPLOADED)
969 element.mObserver->UploadComplete(true, textureInfo.textureId, textureInfo.textureSet, textureInfo.useAtlas, textureInfo.atlasRect, textureInfo.preMultiplied);
971 else if(textureInfo.loadState == LoadState::LOAD_FINISHED && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
973 element.mObserver->LoadComplete(true, textureInfo.pixelBuffer, textureInfo.url, textureInfo.preMultiplied);
977 LoadTexture(textureInfo, element.mObserver);
984 void TextureManager::ObserveTexture(TextureInfo& textureInfo,
985 TextureUploadObserver* observer)
987 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ObserveTexture(): url:%s observer:%p\n", textureInfo.url.GetUrl().c_str(), observer);
991 textureInfo.observerList.PushBack(observer);
992 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
996 void TextureManager::AsyncLoadComplete(AsyncLoadingInfoContainerType& loadingContainer, uint32_t id, Devel::PixelBuffer pixelBuffer)
998 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::AsyncLoadComplete( id:%d )\n", id);
1000 if(loadingContainer.size() >= 1u)
1002 AsyncLoadingInfo loadingInfo = loadingContainer.front();
1004 if(loadingInfo.loadId == id)
1006 int cacheIndex = GetCacheIndexFromId(loadingInfo.textureId);
1007 if(cacheIndex != INVALID_CACHE_INDEX)
1009 TextureInfo& textureInfo(mTextureInfoContainer[cacheIndex]);
1011 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);
1013 if(textureInfo.loadState != LoadState::CANCELLED)
1015 // textureInfo can be invalidated after this call (as the mTextureInfoContainer may be modified)
1016 PostLoad(textureInfo, pixelBuffer);
1020 Remove(textureInfo.textureId, nullptr);
1025 loadingContainer.pop_front();
1029 void TextureManager::PostLoad(TextureInfo& textureInfo, Devel::PixelBuffer& pixelBuffer)
1031 // Was the load successful?
1032 if(pixelBuffer && (pixelBuffer.GetWidth() != 0) && (pixelBuffer.GetHeight() != 0))
1034 // No atlas support for now
1035 textureInfo.useAtlas = NO_ATLAS;
1036 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
1038 if(textureInfo.storageType == StorageType::UPLOAD_TO_TEXTURE)
1040 // If there is a mask texture ID associated with this texture, then apply the mask
1041 // if it's already loaded. If it hasn't, and the mask is still loading,
1042 // wait for the mask to finish loading.
1043 if(textureInfo.maskTextureId != INVALID_TEXTURE_ID)
1045 if(textureInfo.loadState == LoadState::MASK_APPLYING)
1047 textureInfo.loadState = LoadState::MASK_APPLIED;
1048 UploadTexture(pixelBuffer, textureInfo);
1049 NotifyObservers(textureInfo, true);
1053 LoadState maskLoadState = GetTextureStateInternal(textureInfo.maskTextureId);
1054 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
1055 if(maskLoadState == LoadState::LOADING)
1057 textureInfo.loadState = LoadState::WAITING_FOR_MASK;
1059 else if(maskLoadState == LoadState::LOAD_FINISHED)
1061 // Send New Task to Thread
1062 ApplyMask(textureInfo, textureInfo.maskTextureId);
1068 UploadTexture(pixelBuffer, textureInfo);
1069 NotifyObservers(textureInfo, true);
1074 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
1075 textureInfo.loadState = LoadState::LOAD_FINISHED;
1077 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1079 NotifyObservers(textureInfo, true);
1083 // Check if there was another texture waiting for this load to complete
1084 // (e.g. if this was an image mask, and its load is on a different thread)
1085 CheckForWaitingTexture(textureInfo);
1091 textureInfo.loadState = LoadState::LOAD_FAILED;
1092 CheckForWaitingTexture(textureInfo);
1093 NotifyObservers(textureInfo, false);
1097 void TextureManager::CheckForWaitingTexture(TextureInfo& maskTextureInfo)
1099 // Search the cache, checking if any texture has this texture id as a
1101 const unsigned int size = mTextureInfoContainer.size();
1103 for(unsigned int cacheIndex = 0; cacheIndex < size; ++cacheIndex)
1105 if(mTextureInfoContainer[cacheIndex].maskTextureId == maskTextureInfo.textureId &&
1106 mTextureInfoContainer[cacheIndex].loadState == LoadState::WAITING_FOR_MASK)
1108 TextureInfo& textureInfo(mTextureInfoContainer[cacheIndex]);
1110 if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED)
1112 // Send New Task to Thread
1113 ApplyMask(textureInfo, maskTextureInfo.textureId);
1117 textureInfo.pixelBuffer.Reset();
1118 textureInfo.loadState = LoadState::LOAD_FAILED;
1119 NotifyObservers(textureInfo, false);
1125 void TextureManager::ApplyMask(TextureInfo& textureInfo, TextureId maskTextureId)
1127 int maskCacheIndex = GetCacheIndexFromId(maskTextureId);
1128 if(maskCacheIndex != INVALID_CACHE_INDEX)
1130 Devel::PixelBuffer maskPixelBuffer = mTextureInfoContainer[maskCacheIndex].pixelBuffer;
1131 Devel::PixelBuffer pixelBuffer = textureInfo.pixelBuffer;
1132 textureInfo.pixelBuffer.Reset();
1134 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ApplyMask(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
1136 textureInfo.loadState = LoadState::MASK_APPLYING;
1137 auto& loadersContainer = (textureInfo.url.IsLocalResource() || textureInfo.url.IsBufferResource()) ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
1138 auto loadingHelperIt = loadersContainer.GetNext();
1139 auto premultiplyOnLoad = textureInfo.preMultiplyOnLoad ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
1140 DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
1141 loadingHelperIt->ApplyMask(textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask, premultiplyOnLoad);
1145 void TextureManager::UploadTexture(Devel::PixelBuffer& pixelBuffer, TextureInfo& textureInfo)
1147 if(textureInfo.useAtlas != USE_ATLAS)
1149 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, " TextureManager::UploadTexture() New Texture for textureId:%d\n", textureInfo.textureId);
1151 // Check if this pixelBuffer is premultiplied
1152 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
1154 auto& renderingAddOn = RenderingAddOn::Get();
1155 if(renderingAddOn.IsValid())
1157 renderingAddOn.CreateGeometry(textureInfo.textureId, pixelBuffer);
1160 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight());
1162 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
1163 texture.Upload(pixelData);
1164 if(!textureInfo.textureSet)
1166 textureInfo.textureSet = TextureSet::New();
1168 textureInfo.textureSet.SetTexture(0u, texture);
1171 // Update the load state.
1172 // Note: This is regardless of success as we care about whether a
1173 // load attempt is in progress or not. If unsuccessful, a broken
1174 // image is still loaded.
1175 textureInfo.loadState = LoadState::UPLOADED;
1178 void TextureManager::NotifyObservers(TextureInfo& textureInfo, bool success)
1180 TextureId textureId = textureInfo.textureId;
1182 // If there is an observer: Notify the load is complete, whether successful or not,
1183 // and erase it from the list
1184 TextureInfo* info = &textureInfo;
1186 mQueueLoadFlag = true;
1188 while(info->observerList.Count())
1190 TextureUploadObserver* observer = info->observerList[0];
1192 // During UploadComplete() a Control ResourceReady() signal is emitted.
1193 // During that signal the app may add remove /add Textures (e.g. via
1195 // It is possible for observers to be removed from the observer list,
1196 // and it is also possible for the mTextureInfoContainer to be modified,
1197 // invalidating the reference to the textureInfo struct.
1198 // Texture load requests for the same URL are deferred until the end of this
1200 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "NotifyObservers() url:%s loadState:%s\n", textureInfo.url.GetUrl().c_str(), GET_LOAD_STATE_STRING(textureInfo.loadState));
1202 // It is possible for the observer to be deleted.
1203 // Disconnect and remove the observer first.
1204 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1206 info->observerList.Erase(info->observerList.begin());
1208 if(info->storageType == StorageType::RETURN_PIXEL_BUFFER)
1210 observer->LoadComplete(success, info->pixelBuffer, info->url, info->preMultiplied);
1214 observer->UploadComplete(success, info->textureId, info->textureSet, info->useAtlas, info->atlasRect, info->preMultiplied);
1217 // Get the textureInfo from the container again as it may have been invalidated.
1218 int textureInfoIndex = GetCacheIndexFromId(textureId);
1219 if(textureInfoIndex == INVALID_CACHE_INDEX)
1221 break; // texture has been removed - can stop.
1223 info = &mTextureInfoContainer[textureInfoIndex];
1226 mQueueLoadFlag = false;
1227 ProcessQueuedTextures();
1229 if(info->storageType == StorageType::RETURN_PIXEL_BUFFER && info->observerList.Count() == 0)
1231 Remove(info->textureId, nullptr);
1235 TextureManager::TextureId TextureManager::GenerateUniqueTextureId()
1237 return mCurrentTextureId++;
1240 int TextureManager::GetCacheIndexFromId(const TextureId textureId)
1242 const unsigned int size = mTextureInfoContainer.size();
1244 for(unsigned int i = 0; i < size; ++i)
1246 if(mTextureInfoContainer[i].textureId == textureId)
1252 return INVALID_CACHE_INDEX;
1255 TextureManager::TextureHash TextureManager::GenerateHash(
1256 const std::string& url,
1257 const ImageDimensions size,
1258 const FittingMode::Type fittingMode,
1259 const Dali::SamplingMode::Type samplingMode,
1260 const UseAtlas useAtlas,
1261 TextureId maskTextureId)
1263 std::string hashTarget(url);
1264 const size_t urlLength = hashTarget.length();
1265 const uint16_t width = size.GetWidth();
1266 const uint16_t height = size.GetWidth();
1268 // If either the width or height has been specified, include the resizing options in the hash
1269 if(width != 0 || height != 0)
1271 // We are appending 5 bytes to the URL to form the hash input.
1272 hashTarget.resize(urlLength + 5u);
1273 char* hashTargetPtr = &(hashTarget[urlLength]);
1275 // Pack the width and height (4 bytes total).
1276 *hashTargetPtr++ = size.GetWidth() & 0xff;
1277 *hashTargetPtr++ = (size.GetWidth() >> 8u) & 0xff;
1278 *hashTargetPtr++ = size.GetHeight() & 0xff;
1279 *hashTargetPtr++ = (size.GetHeight() >> 8u) & 0xff;
1281 // Bit-pack the FittingMode, SamplingMode and atlasing.
1282 // FittingMode=2bits, SamplingMode=3bits, useAtlas=1bit
1283 *hashTargetPtr = (fittingMode << 4u) | (samplingMode << 1) | useAtlas;
1287 // We are not including sizing information, but we still need an extra byte for atlasing.
1288 hashTarget.resize(urlLength + 1u);
1290 // Add the atlasing to the hash input.
1293 case UseAtlas::NO_ATLAS:
1295 hashTarget[urlLength] = 'f';
1298 case UseAtlas::USE_ATLAS:
1300 hashTarget[urlLength] = 't';
1306 if(maskTextureId != INVALID_TEXTURE_ID)
1308 auto textureIdIndex = hashTarget.length();
1309 hashTarget.resize(hashTarget.length() + sizeof(TextureId));
1310 unsigned char* hashTargetPtr = reinterpret_cast<unsigned char*>(&(hashTarget[textureIdIndex]));
1312 // Append the texture id to the end of the URL byte by byte:
1313 // (to avoid SIGBUS / alignment issues)
1314 for(size_t byteIter = 0; byteIter < sizeof(TextureId); ++byteIter)
1316 *hashTargetPtr++ = maskTextureId & 0xff;
1317 maskTextureId >>= 8u;
1321 return Dali::CalculateHash(hashTarget);
1324 int TextureManager::FindCachedTexture(
1325 const TextureManager::TextureHash hash,
1326 const std::string& url,
1327 const ImageDimensions size,
1328 const FittingMode::Type fittingMode,
1329 const Dali::SamplingMode::Type samplingMode,
1330 const bool useAtlas,
1331 TextureId maskTextureId,
1332 TextureManager::MultiplyOnLoad preMultiplyOnLoad)
1334 // Default to an invalid ID, in case we do not find a match.
1335 int cacheIndex = INVALID_CACHE_INDEX;
1337 // Iterate through our hashes to find a match.
1338 const unsigned int count = mTextureInfoContainer.size();
1339 for(unsigned int i = 0u; i < count; ++i)
1341 if(mTextureInfoContainer[i].hash == hash)
1343 // We have a match, now we check all the original parameters in case of a hash collision.
1344 TextureInfo& textureInfo(mTextureInfoContainer[i]);
1346 if((url == textureInfo.url.GetUrl()) &&
1347 (useAtlas == textureInfo.useAtlas) &&
1348 (maskTextureId == textureInfo.maskTextureId) &&
1349 (size == textureInfo.desiredSize) &&
1350 ((size.GetWidth() == 0 && size.GetHeight() == 0) ||
1351 (fittingMode == textureInfo.fittingMode &&
1352 samplingMode == textureInfo.samplingMode)))
1354 // 1. If preMultiplyOnLoad is MULTIPLY_ON_LOAD, then textureInfo.preMultiplyOnLoad should be true. The premultiplication result can be different.
1355 // 2. If preMultiplyOnLoad is LOAD_WITHOUT_MULTIPLY, then textureInfo.preMultiplied should be false.
1356 if((preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD && textureInfo.preMultiplyOnLoad) || (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY && !textureInfo.preMultiplied))
1358 // The found Texture is a match.
1369 void TextureManager::ObserverDestroyed(TextureUploadObserver* observer)
1371 const unsigned int count = mTextureInfoContainer.size();
1372 for(unsigned int i = 0; i < count; ++i)
1374 TextureInfo& textureInfo(mTextureInfoContainer[i]);
1375 for(TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin();
1376 j != textureInfo.observerList.End();)
1380 j = textureInfo.observerList.Erase(j);
1389 // Remove element from the LoadQueue
1390 for(auto&& element : mLoadQueue)
1392 if(element.mObserver == observer)
1394 element.mObserver = nullptr;
1399 TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(TextureManager& textureManager)
1400 : AsyncLoadingHelper(Toolkit::AsyncImageLoader::New(), textureManager, AsyncLoadingInfoContainerType())
1404 void TextureManager::AsyncLoadingHelper::LoadAnimatedImage(TextureId textureId,
1405 Dali::AnimatedImageLoading animatedImageLoading,
1406 uint32_t frameIndex)
1408 mLoadingInfoContainer.push_back(AsyncLoadingInfo(textureId));
1409 auto id = GetImplementation(mLoader).LoadAnimatedImage(animatedImageLoading, frameIndex);
1410 mLoadingInfoContainer.back().loadId = id;
1413 void TextureManager::AsyncLoadingHelper::Load(TextureId textureId,
1414 const VisualUrl& url,
1415 ImageDimensions desiredSize,
1416 FittingMode::Type fittingMode,
1417 SamplingMode::Type samplingMode,
1418 bool orientationCorrection,
1419 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
1421 mLoadingInfoContainer.push_back(AsyncLoadingInfo(textureId));
1422 if(DALI_UNLIKELY(url.IsBufferResource()))
1424 auto id = GetImplementation(mLoader).LoadEncodedImageBuffer(mTextureManager.GetEncodedImageBuffer(url.GetUrl()), desiredSize, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad);
1425 mLoadingInfoContainer.back().loadId = id;
1429 auto id = GetImplementation(mLoader).Load(url, desiredSize, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad);
1430 mLoadingInfoContainer.back().loadId = id;
1434 void TextureManager::AsyncLoadingHelper::ApplyMask(TextureId textureId,
1435 Devel::PixelBuffer pixelBuffer,
1436 Devel::PixelBuffer maskPixelBuffer,
1439 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
1441 mLoadingInfoContainer.push_back(AsyncLoadingInfo(textureId));
1442 auto id = GetImplementation(mLoader).ApplyMask(pixelBuffer, maskPixelBuffer, contentScale, cropToMask, preMultiplyOnLoad);
1443 mLoadingInfoContainer.back().loadId = id;
1446 TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(AsyncLoadingHelper&& rhs)
1447 : AsyncLoadingHelper(rhs.mLoader, rhs.mTextureManager, std::move(rhs.mLoadingInfoContainer))
1451 TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(
1452 Toolkit::AsyncImageLoader loader,
1453 TextureManager& textureManager,
1454 AsyncLoadingInfoContainerType&& loadingInfoContainer)
1456 mTextureManager(textureManager),
1457 mLoadingInfoContainer(std::move(loadingInfoContainer))
1459 DevelAsyncImageLoader::PixelBufferLoadedSignal(mLoader).Connect(
1460 this, &AsyncLoadingHelper::AsyncLoadComplete);
1463 void TextureManager::AsyncLoadingHelper::AsyncLoadComplete(uint32_t id,
1464 Devel::PixelBuffer pixelBuffer)
1466 mTextureManager.AsyncLoadComplete(mLoadingInfoContainer, id, pixelBuffer);
1469 Geometry TextureManager::GetRenderGeometry(TextureId textureId, uint32_t& frontElements, uint32_t& backElements)
1471 return RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().GetGeometry(textureId, frontElements, backElements) : Geometry();
1474 } // namespace Internal
1476 } // namespace Toolkit