2 * Copyright (c) 2022 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 "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/integration-api/debug.h>
26 #include <dali/public-api/rendering/geometry.h>
29 #include <dali-toolkit/internal/texture-manager/texture-async-loading-helper.h>
30 #include <dali-toolkit/internal/texture-manager/texture-cache-manager.h>
31 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
32 #include <dali-toolkit/internal/visuals/rendering-addon.h>
36 constexpr auto INITIAL_HASH_NUMBER = size_t{0u};
37 constexpr auto DEFAULT_NUMBER_OF_LOCAL_LOADER_THREADS = size_t{4u};
38 constexpr auto DEFAULT_NUMBER_OF_REMOTE_LOADER_THREADS = size_t{8u};
40 constexpr auto NUMBER_OF_LOCAL_LOADER_THREADS_ENV = "DALI_TEXTURE_LOCAL_THREADS";
41 constexpr auto NUMBER_OF_REMOTE_LOADER_THREADS_ENV = "DALI_TEXTURE_REMOTE_THREADS";
43 size_t GetNumberOfThreads(const char* environmentVariable, size_t defaultValue)
45 using Dali::EnvironmentVariable::GetEnvironmentVariable;
46 auto numberString = GetEnvironmentVariable(environmentVariable);
47 auto numberOfThreads = numberString ? std::strtoul(numberString, nullptr, 10) : 0;
48 constexpr auto MAX_NUMBER_OF_THREADS = 100u;
49 DALI_ASSERT_DEBUG(numberOfThreads < MAX_NUMBER_OF_THREADS);
50 return (numberOfThreads > 0 && numberOfThreads < MAX_NUMBER_OF_THREADS) ? numberOfThreads : defaultValue;
53 size_t GetNumberOfLocalLoaderThreads()
55 return GetNumberOfThreads(NUMBER_OF_LOCAL_LOADER_THREADS_ENV, DEFAULT_NUMBER_OF_LOCAL_LOADER_THREADS);
58 size_t GetNumberOfRemoteLoaderThreads()
60 return GetNumberOfThreads(NUMBER_OF_REMOTE_LOADER_THREADS_ENV, DEFAULT_NUMBER_OF_REMOTE_LOADER_THREADS);
72 // Define logfilter Internal namespace level so other files (e.g. texture-cache-manager.cpp) can also use this filter.
73 Debug::Filter* gTextureManagerLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TEXTURE_MANAGER");
76 #define GET_LOAD_STATE_STRING(loadState) \
77 loadState == TextureManagerType::LoadState::NOT_STARTED ? "NOT_STARTED" : \
78 loadState == TextureManagerType::LoadState::LOADING ? "LOADING" : \
79 loadState == TextureManagerType::LoadState::LOAD_FINISHED ? "LOAD_FINISHED" : \
80 loadState == TextureManagerType::LoadState::WAITING_FOR_MASK ? "WAITING_FOR_MASK" : \
81 loadState == TextureManagerType::LoadState::MASK_APPLYING ? "MASK_APPLYING" : \
82 loadState == TextureManagerType::LoadState::MASK_APPLIED ? "MASK_APPLIED" : \
83 loadState == TextureManagerType::LoadState::UPLOADED ? "UPLOADED" : \
84 loadState == TextureManagerType::LoadState::CANCELLED ? "CANCELLED" : \
85 loadState == TextureManagerType::LoadState::LOAD_FAILED ? "LOAD_FAILED" : \
92 const uint32_t DEFAULT_ATLAS_SIZE(1024u); ///< This size can fit 8 by 8 images of average size 128 * 128
93 const Vector4 FULL_ATLAS_RECT(0.0f, 0.0f, 1.0f, 1.0f); ///< UV Rectangle that covers the full Texture
95 void PreMultiply(Devel::PixelBuffer pixelBuffer, TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
97 if(Pixel::HasAlpha(pixelBuffer.GetPixelFormat()))
99 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
101 pixelBuffer.MultiplyColorByAlpha();
106 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
110 } // Anonymous namespace
112 TextureManager::MaskingData::MaskingData()
114 mAlphaMaskId(INVALID_TEXTURE_ID),
115 mContentScaleFactor(1.0f),
120 TextureManager::TextureManager()
121 : mTextureCacheManager(),
122 mAsyncLocalLoaders(GetNumberOfLocalLoaderThreads(), [&]() { return TextureAsyncLoadingHelper(*this); }),
123 mAsyncRemoteLoaders(GetNumberOfRemoteLoaderThreads(), [&]() { return TextureAsyncLoadingHelper(*this); }),
124 mLifecycleObservers(),
127 mQueueLoadFlag(false)
129 // Initialize the AddOn
130 RenderingAddOn::Get();
133 TextureManager::~TextureManager()
135 for(auto iter = mLifecycleObservers.Begin(), endIter = mLifecycleObservers.End(); iter != endIter; ++iter)
137 (*iter)->TextureManagerDestroyed();
141 TextureSet TextureManager::LoadAnimatedImageTexture(
142 Dali::AnimatedImageLoading animatedImageLoading,
143 const uint32_t& frameIndex,
144 TextureManager::TextureId& textureId,
145 MaskingDataPointer& maskInfo,
146 const Dali::SamplingMode::Type& samplingMode,
147 const Dali::WrapMode::Type& wrapModeU,
148 const Dali::WrapMode::Type& wrapModeV,
149 const bool& synchronousLoading,
150 TextureUploadObserver* textureObserver)
152 TextureSet textureSet;
154 if(synchronousLoading)
156 Devel::PixelBuffer pixelBuffer;
157 if(animatedImageLoading)
159 pixelBuffer = animatedImageLoading.LoadFrame(frameIndex);
163 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous loading is failed\n");
167 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
169 Devel::PixelBuffer maskPixelBuffer = LoadImageFromFile(maskInfo->mAlphaMaskUrl.GetUrl(), ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true);
172 pixelBuffer.ApplyMask(maskPixelBuffer, maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
176 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous mask image loading is failed\n");
179 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
182 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight());
183 texture.Upload(pixelData);
184 textureSet = TextureSet::New();
185 textureSet.SetTexture(0u, texture);
191 TextureId alphaMaskId = INVALID_TEXTURE_ID;
192 float contentScaleFactor = 1.0f;
193 bool cropToMask = false;
194 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
196 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl);
197 alphaMaskId = maskInfo->mAlphaMaskId;
198 contentScaleFactor = maskInfo->mContentScaleFactor;
199 cropToMask = maskInfo->mCropToMask;
202 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
203 textureId = RequestLoadInternal(animatedImageLoading.GetUrl(), alphaMaskId, contentScaleFactor, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, UseAtlas::NO_ATLAS, cropToMask, StorageType::UPLOAD_TO_TEXTURE, textureObserver, true, TextureManager::ReloadPolicy::CACHED, preMultiply, animatedImageLoading, frameIndex, false);
205 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
206 if(loadState == TextureManager::LoadState::UPLOADED)
208 // LoadComplete has already been called - keep the same texture set
209 textureSet = GetTextureSet(textureId);
215 Sampler sampler = Sampler::New();
216 sampler.SetWrapMode(wrapModeU, wrapModeV);
217 textureSet.SetSampler(0u, sampler);
223 Devel::PixelBuffer TextureManager::LoadPixelBuffer(
224 const VisualUrl& url,
225 const Dali::ImageDimensions& desiredSize,
226 const Dali::FittingMode::Type& fittingMode,
227 const Dali::SamplingMode::Type& samplingMode,
228 const bool& synchronousLoading,
229 TextureUploadObserver* textureObserver,
230 const bool& orientationCorrection,
231 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
233 Devel::PixelBuffer pixelBuffer;
234 if(synchronousLoading)
238 if(url.IsBufferResource())
240 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
241 if(encodedImageBuffer)
243 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
248 pixelBuffer = LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
250 if(pixelBuffer && preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
252 PreMultiply(pixelBuffer, preMultiplyOnLoad);
258 RequestLoadInternal(url, INVALID_TEXTURE_ID, 1.0f, desiredSize, fittingMode, samplingMode, UseAtlas::NO_ATLAS, false, StorageType::RETURN_PIXEL_BUFFER, textureObserver, orientationCorrection, TextureManager::ReloadPolicy::FORCED, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, false);
264 TextureSet TextureManager::LoadTexture(
265 const VisualUrl& url,
266 const Dali::ImageDimensions& desiredSize,
267 const Dali::FittingMode::Type& fittingMode,
268 const Dali::SamplingMode::Type& samplingMode,
269 MaskingDataPointer& maskInfo,
270 const bool& synchronousLoading,
271 TextureManager::TextureId& textureId,
272 Vector4& textureRect,
273 Dali::ImageDimensions& textureRectSize,
274 bool& atlasingStatus,
276 const Dali::WrapMode::Type& wrapModeU,
277 const Dali::WrapMode::Type& wrapModeV,
278 TextureUploadObserver* textureObserver,
279 AtlasUploadObserver* atlasObserver,
280 ImageAtlasManagerPtr imageAtlasManager,
281 const bool& orientationCorrection,
282 const TextureManager::ReloadPolicy& reloadPolicy,
283 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
285 TextureSet textureSet;
287 loadingStatus = false;
288 textureRect = FULL_ATLAS_RECT;
290 if(VisualUrl::TEXTURE == url.GetProtocolType())
292 std::string location = url.GetLocation();
293 if(location.size() > 0u)
295 TextureId id = std::stoi(location);
296 textureSet = mTextureCacheManager.GetExternalTextureSet(id);
299 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
308 if(synchronousLoading && atlasingStatus && imageAtlasManager->CheckAtlasAvailable(url, desiredSize))
310 Devel::PixelBuffer pixelBuffer = LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection);
312 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
314 Devel::PixelBuffer maskPixelBuffer = LoadImageSynchronously(maskInfo->mAlphaMaskUrl.GetUrl(), ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true);
317 pixelBuffer.ApplyMask(maskPixelBuffer, maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
324 PreMultiply(pixelBuffer, preMultiplyOnLoad);
325 data = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
329 textureSet = imageAtlasManager->Add(textureRect, data);
332 textureRectSize.SetWidth(data.GetWidth());
333 textureRectSize.SetHeight(data.GetHeight());
338 DALI_LOG_ERROR("TextureManager::LoadTexture: Synchronous Texture loading with atlasing is failed.\n");
343 atlasingStatus = false;
349 loadingStatus = true;
350 // Atlas manager can chage desired size when it is set by 0,0.
351 // We should store into textureRectSize only if atlasing successed.
352 // So copy inputed desiredSize, and replace value into textureRectSize only if atlasing success.
353 Dali::ImageDimensions atlasDesiredSize = desiredSize;
356 textureSet = imageAtlasManager->Add(textureRect, url.GetUrl(), atlasDesiredSize, fittingMode, true, atlasObserver);
358 if(!textureSet) // big image, no atlasing or atlasing failed
360 atlasingStatus = false;
361 if(!maskInfo || !maskInfo->mAlphaMaskUrl.IsValid())
363 textureId = RequestLoad(
370 orientationCorrection,
377 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, synchronousLoading);
378 textureId = RequestLoad(
380 maskInfo->mAlphaMaskId,
381 maskInfo->mContentScaleFactor,
386 maskInfo->mCropToMask,
388 orientationCorrection,
394 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
395 if(loadState == TextureManager::LoadState::UPLOADED)
397 // LoadComplete has already been called - keep the same texture set
398 textureSet = mTextureCacheManager.GetTextureSet(textureId);
401 // If we are loading the texture, or waiting for the ready signal handler to complete, inform
402 // caller that they need to wait.
403 loadingStatus = (loadState == TextureManager::LoadState::LOADING ||
404 loadState == TextureManager::LoadState::WAITING_FOR_MASK ||
405 loadState == TextureManager::LoadState::MASK_APPLYING ||
406 loadState == TextureManager::LoadState::MASK_APPLIED ||
407 loadState == TextureManager::LoadState::NOT_STARTED ||
412 textureRectSize = atlasDesiredSize;
417 if(!atlasingStatus && textureSet)
419 Sampler sampler = Sampler::New();
420 sampler.SetWrapMode(wrapModeU, wrapModeV);
421 textureSet.SetSampler(0u, sampler);
424 if(synchronousLoading)
426 loadingStatus = false;
432 TextureManager::TextureId TextureManager::RequestLoad(
433 const VisualUrl& url,
434 const ImageDimensions& desiredSize,
435 const Dali::FittingMode::Type& fittingMode,
436 const Dali::SamplingMode::Type& samplingMode,
437 const UseAtlas& useAtlas,
438 TextureUploadObserver* observer,
439 const bool& orientationCorrection,
440 const TextureManager::ReloadPolicy& reloadPolicy,
441 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
442 const bool& synchronousLoading)
444 return RequestLoadInternal(url, INVALID_TEXTURE_ID, 1.0f, desiredSize, fittingMode, samplingMode, useAtlas, false, StorageType::UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
447 TextureManager::TextureId TextureManager::RequestLoad(
448 const VisualUrl& url,
449 const TextureManager::TextureId& maskTextureId,
450 const float& contentScale,
451 const Dali::ImageDimensions& desiredSize,
452 const Dali::FittingMode::Type& fittingMode,
453 const Dali::SamplingMode::Type& samplingMode,
454 const TextureManager::UseAtlas& useAtlas,
455 const bool& cropToMask,
456 TextureUploadObserver* observer,
457 const bool& orientationCorrection,
458 const TextureManager::ReloadPolicy& reloadPolicy,
459 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
460 const bool& synchronousLoading)
462 return RequestLoadInternal(url, maskTextureId, contentScale, desiredSize, fittingMode, samplingMode, useAtlas, cropToMask, StorageType::UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
465 TextureManager::TextureId TextureManager::RequestMaskLoad(
466 const VisualUrl& maskUrl,
467 const bool& synchronousLoading)
469 // Use the normal load procedure to get the alpha mask.
470 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
471 return RequestLoadInternal(maskUrl, INVALID_TEXTURE_ID, 1.0f, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, UseAtlas::NO_ATLAS, false, StorageType::KEEP_PIXEL_BUFFER, NULL, true, TextureManager::ReloadPolicy::CACHED, preMultiply, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
474 TextureManager::TextureId TextureManager::RequestLoadInternal(
475 const VisualUrl& url,
476 const TextureManager::TextureId& maskTextureId,
477 const float& contentScale,
478 const Dali::ImageDimensions& desiredSize,
479 const Dali::FittingMode::Type& fittingMode,
480 const Dali::SamplingMode::Type& samplingMode,
481 const TextureManager::UseAtlas& useAtlas,
482 const bool& cropToMask,
483 const TextureManager::StorageType& storageType,
484 TextureUploadObserver* observer,
485 const bool& orientationCorrection,
486 const TextureManager::ReloadPolicy& reloadPolicy,
487 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
488 Dali::AnimatedImageLoading animatedImageLoading,
489 const std::uint32_t& frameIndex,
490 const bool& synchronousLoading)
492 TextureHash textureHash = INITIAL_HASH_NUMBER;
493 TextureCacheIndex cacheIndex = INVALID_CACHE_INDEX;
494 if(storageType != StorageType::RETURN_PIXEL_BUFFER)
496 textureHash = mTextureCacheManager.GenerateHash(url, desiredSize, fittingMode, samplingMode, useAtlas, maskTextureId, cropToMask, frameIndex);
498 // Look up the texture by hash. Note: The extra parameters are used in case of a hash collision.
499 cacheIndex = mTextureCacheManager.FindCachedTexture(textureHash, url, desiredSize, fittingMode, samplingMode, useAtlas, maskTextureId, cropToMask, preMultiplyOnLoad, (animatedImageLoading) ? true : false, frameIndex);
502 TextureManager::TextureId textureId = INVALID_TEXTURE_ID;
503 // Check if the requested Texture exists in the cache.
504 if(cacheIndex != INVALID_CACHE_INDEX)
506 if(TextureManager::ReloadPolicy::CACHED == reloadPolicy)
508 // Mark this texture being used by another client resource. Forced reload would replace the current texture
509 // without the need for incrementing the reference count.
510 ++(mTextureCacheManager[cacheIndex].referenceCount);
512 textureId = mTextureCacheManager[cacheIndex].textureId;
514 // Update preMultiplyOnLoad value. It should be changed according to preMultiplied value of the cached info.
515 preMultiplyOnLoad = mTextureCacheManager[cacheIndex].preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
517 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::RequestLoad( url=%s observer=%p ) Using cached texture id@%d, textureId=%d, frameindex=%d, premultiplied=%d\n", url.GetUrl().c_str(), observer, cacheIndex.GetIndex(), textureId, frameIndex, mTextureCacheManager[cacheIndex].preMultiplied ? 1 : 0);
520 if(textureId == INVALID_TEXTURE_ID) // There was no caching, or caching not required
522 textureId = mTextureCacheManager.GenerateTextureId();
524 bool preMultiply = (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
526 // Cache new texutre, and get cacheIndex.
527 cacheIndex = mTextureCacheManager.AppendCache(TextureInfo(textureId, maskTextureId, url, desiredSize, contentScale, fittingMode, samplingMode, false, cropToMask, useAtlas, textureHash, orientationCorrection, preMultiply, animatedImageLoading, frameIndex));
529 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::RequestLoad( url=%s observer=%p ) New texture, cacheIndex:%d, textureId=%d, frameindex=%d\n", url.GetUrl().c_str(), observer, cacheIndex.GetIndex(), textureId, frameIndex);
532 // The below code path is common whether we are using the cache or not.
533 // The textureInfoIndex now refers to either a pre-existing cached TextureInfo,
534 // or a new TextureInfo just created.
535 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
536 textureInfo.maskTextureId = maskTextureId;
537 textureInfo.storageType = storageType;
538 textureInfo.orientationCorrection = orientationCorrection;
540 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureInfo loadState:%s\n", GET_LOAD_STATE_STRING(textureInfo.loadState));
542 // Force reloading of texture by setting loadState unless already loading or cancelled.
543 if(TextureManager::ReloadPolicy::FORCED == reloadPolicy &&
544 TextureManager::LoadState::LOADING != textureInfo.loadState &&
545 TextureManager::LoadState::WAITING_FOR_MASK != textureInfo.loadState &&
546 TextureManager::LoadState::MASK_APPLYING != textureInfo.loadState &&
547 TextureManager::LoadState::MASK_APPLIED != textureInfo.loadState &&
548 TextureManager::LoadState::CANCELLED != textureInfo.loadState)
550 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, "TextureManager::RequestLoad( url=%s observer=%p ) ForcedReload cacheIndex:%d, textureId=%d\n", url.GetUrl().c_str(), observer, cacheIndex.GetIndex(), textureId);
552 textureInfo.loadState = TextureManager::LoadState::NOT_STARTED;
555 if(!synchronousLoading)
557 // Check if we should add the observer.
558 // Only do this if we have not loaded yet and it will not have loaded by the end of this method.
559 switch(textureInfo.loadState)
561 case TextureManager::LoadState::LOAD_FAILED: // Failed notifies observer which then stops observing.
562 case TextureManager::LoadState::NOT_STARTED:
564 LoadOrQueueTexture(textureInfo, observer); // If called inside NotifyObservers, queues until afterwards
567 case TextureManager::LoadState::LOADING:
568 case TextureManager::LoadState::WAITING_FOR_MASK:
569 case TextureManager::LoadState::MASK_APPLYING:
570 case TextureManager::LoadState::MASK_APPLIED:
572 ObserveTexture(textureInfo, observer);
575 case TextureManager::LoadState::UPLOADED:
579 LoadOrQueueTexture(textureInfo, observer);
583 case TextureManager::LoadState::CANCELLED:
585 // A cancelled texture hasn't finished loading yet. Treat as a loading texture
586 // (it's ref count has already been incremented, above)
587 textureInfo.loadState = TextureManager::LoadState::LOADING;
588 ObserveTexture(textureInfo, observer);
591 case TextureManager::LoadState::LOAD_FINISHED:
593 // Loading has already completed.
594 if(observer && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
596 LoadOrQueueTexture(textureInfo, observer);
604 // If the image is already finished to load, use cached texture.
605 // We don't need to consider Observer because this is synchronous loading.
606 if(!(textureInfo.loadState == TextureManager::LoadState::UPLOADED ||
607 textureInfo.loadState == TextureManager::LoadState::LOAD_FINISHED))
609 Devel::PixelBuffer pixelBuffer = LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection);
613 // If pixelBuffer loading is failed in synchronously, call Remove() method.
614 Remove(textureId, nullptr);
615 return INVALID_TEXTURE_ID;
618 if(storageType == StorageType::KEEP_PIXEL_BUFFER) // For the mask image loading.
620 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
621 textureInfo.loadState = LoadState::LOAD_FINISHED;
623 else // For the image loading.
625 if(maskTextureId != INVALID_TEXTURE_ID)
627 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
628 if(maskCacheIndex != INVALID_CACHE_INDEX)
630 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
633 pixelBuffer.ApplyMask(maskPixelBuffer, contentScale, cropToMask);
637 DALI_LOG_ERROR("Mask image cached invalid pixel buffer!\n");
642 DALI_LOG_ERROR("Mask image is not stored in cache.\n");
645 PreMultiply(pixelBuffer, preMultiplyOnLoad);
648 UploadTexture(pixelBuffer, textureInfo);
655 void TextureManager::Remove(const TextureManager::TextureId& textureId, TextureUploadObserver* observer)
657 if(textureId != INVALID_TEXTURE_ID)
661 // Remove textureId after NotifyObserver finished
662 mRemoveQueue.PushBack(textureId);
666 // Remove textureId in CacheManager.
667 mTextureCacheManager.RemoveCache(textureId);
672 // Remove element from the LoadQueue
673 for(auto&& element : mLoadQueue)
675 if(element.mObserver == observer)
677 // Do not erase the item. We will clear it later in ProcessLoadQueue().
678 element.mObserver = nullptr;
686 Devel::PixelBuffer TextureManager::LoadImageSynchronously(
687 const VisualUrl& url,
688 const Dali::ImageDimensions& desiredSize,
689 const Dali::FittingMode::Type& fittingMode,
690 const Dali::SamplingMode::Type& samplingMode,
691 const bool& orientationCorrection)
693 Devel::PixelBuffer pixelBuffer;
694 if(url.IsBufferResource())
696 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
697 if(encodedImageBuffer)
699 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
704 pixelBuffer = LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
709 void TextureManager::AddObserver(TextureManager::LifecycleObserver& observer)
711 // make sure an observer doesn't observe the same object twice
712 // otherwise it will get multiple calls to ObjectDestroyed()
713 DALI_ASSERT_DEBUG(mLifecycleObservers.End() == std::find(mLifecycleObservers.Begin(), mLifecycleObservers.End(), &observer));
714 mLifecycleObservers.PushBack(&observer);
717 void TextureManager::RemoveObserver(TextureManager::LifecycleObserver& observer)
719 // Find the observer...
720 auto endIter = mLifecycleObservers.End();
721 for(auto iter = mLifecycleObservers.Begin(); iter != endIter; ++iter)
723 if((*iter) == &observer)
725 mLifecycleObservers.Erase(iter);
729 DALI_ASSERT_DEBUG(endIter != mLifecycleObservers.End());
732 void TextureManager::LoadOrQueueTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
734 switch(textureInfo.loadState)
736 case LoadState::NOT_STARTED:
737 case LoadState::LOAD_FAILED:
741 QueueLoadTexture(textureInfo, observer);
745 LoadTexture(textureInfo, observer);
749 case LoadState::UPLOADED:
753 QueueLoadTexture(textureInfo, observer);
757 // The Texture has already loaded. The other observers have already been notified.
758 // We need to send a "late" loaded notification for this observer.
759 EmitLoadComplete(observer, textureInfo, true);
763 case LoadState::LOADING:
764 case LoadState::CANCELLED:
765 case LoadState::LOAD_FINISHED:
766 case LoadState::WAITING_FOR_MASK:
767 case LoadState::MASK_APPLYING:
768 case LoadState::MASK_APPLIED:
775 void TextureManager::QueueLoadTexture(const TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
777 const auto& textureId = textureInfo.textureId;
778 mLoadQueue.PushBack(LoadQueueElement(textureId, observer));
780 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
783 void TextureManager::LoadTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
785 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::LoadTexture(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
787 textureInfo.loadState = LoadState::LOADING;
788 if(!textureInfo.loadSynchronously)
790 auto& loadersContainer = (textureInfo.url.IsLocalResource() || textureInfo.url.IsBufferResource()) ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
791 auto loadingHelperIt = loadersContainer.GetNext();
792 auto premultiplyOnLoad = (textureInfo.preMultiplyOnLoad && textureInfo.maskTextureId == INVALID_TEXTURE_ID) ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
793 DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
794 if(textureInfo.animatedImageLoading)
796 loadingHelperIt->LoadAnimatedImage(textureInfo.textureId, textureInfo.animatedImageLoading, textureInfo.frameIndex);
800 loadingHelperIt->Load(textureInfo.textureId, textureInfo.url, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, textureInfo.orientationCorrection, premultiplyOnLoad);
803 ObserveTexture(textureInfo, observer);
806 void TextureManager::ProcessLoadQueue()
808 for(auto&& element : mLoadQueue)
810 if(!element.mObserver)
815 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(element.mTextureId);
816 if(cacheIndex != INVALID_CACHE_INDEX)
818 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
819 if((textureInfo.loadState == LoadState::UPLOADED) || (textureInfo.loadState == LoadState::LOAD_FINISHED && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER))
821 EmitLoadComplete(element.mObserver, textureInfo, true);
825 LoadTexture(textureInfo, element.mObserver);
832 void TextureManager::ProcessRemoveQueue()
834 for(const auto& textureId : mRemoveQueue)
836 mTextureCacheManager.RemoveCache(textureId);
838 mRemoveQueue.Clear();
841 void TextureManager::ObserveTexture(TextureManager::TextureInfo& textureInfo,
842 TextureUploadObserver* observer)
844 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ObserveTexture(): url:%s observer:%p\n", textureInfo.url.GetUrl().c_str(), observer);
848 textureInfo.observerList.PushBack(observer);
849 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
853 void TextureManager::AsyncLoadComplete(const TextureManager::TextureId& textureId, Devel::PixelBuffer pixelBuffer)
855 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
856 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::AsyncLoadComplete( textureId:%d CacheIndex:%d )\n", textureId, cacheIndex.GetIndex());
857 if(cacheIndex != INVALID_CACHE_INDEX)
859 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
861 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, " textureId:%d Url:%s CacheIndex:%d LoadState: %s\n", textureInfo.textureId, textureInfo.url.GetUrl().c_str(), cacheIndex.GetIndex(), GET_LOAD_STATE_STRING(textureInfo.loadState));
863 if(textureInfo.loadState != LoadState::CANCELLED)
865 // textureInfo can be invalidated after this call (as the mTextureInfoContainer may be modified)
866 PostLoad(textureInfo, pixelBuffer);
870 Remove(textureInfo.textureId, nullptr);
875 void TextureManager::PostLoad(TextureManager::TextureInfo& textureInfo, Devel::PixelBuffer& pixelBuffer)
877 // Was the load successful?
878 if(pixelBuffer && (pixelBuffer.GetWidth() != 0) && (pixelBuffer.GetHeight() != 0))
880 // No atlas support for now
881 textureInfo.useAtlas = UseAtlas::NO_ATLAS;
882 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
884 if(textureInfo.storageType == StorageType::UPLOAD_TO_TEXTURE)
886 // If there is a mask texture ID associated with this texture, then apply the mask
887 // if it's already loaded. If it hasn't, and the mask is still loading,
888 // wait for the mask to finish loading.
889 // note, If the texture is already uploaded synchronously during loading,
890 // we don't need to apply mask.
891 if(textureInfo.loadState != LoadState::UPLOADED &&
892 textureInfo.maskTextureId != INVALID_TEXTURE_ID)
894 if(textureInfo.loadState == LoadState::MASK_APPLYING)
896 textureInfo.loadState = LoadState::MASK_APPLIED;
897 UploadTexture(pixelBuffer, textureInfo);
898 NotifyObservers(textureInfo, true);
902 LoadState maskLoadState = mTextureCacheManager.GetTextureStateInternal(textureInfo.maskTextureId);
903 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
904 if(maskLoadState == LoadState::LOADING)
906 textureInfo.loadState = LoadState::WAITING_FOR_MASK;
908 else if(maskLoadState == LoadState::LOAD_FINISHED)
910 // Send New Task to Thread
911 ApplyMask(textureInfo, textureInfo.maskTextureId);
913 else // maskLoadState == LoadState::LOAD_FAILED
915 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
916 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
917 UploadTexture(pixelBuffer, textureInfo);
918 NotifyObservers(textureInfo, true);
924 UploadTexture(pixelBuffer, textureInfo);
925 NotifyObservers(textureInfo, true);
930 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
931 textureInfo.loadState = LoadState::LOAD_FINISHED;
933 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
935 NotifyObservers(textureInfo, true);
939 // Check if there was another texture waiting for this load to complete
940 // (e.g. if this was an image mask, and its load is on a different thread)
941 CheckForWaitingTexture(textureInfo);
947 textureInfo.loadState = LoadState::LOAD_FAILED;
948 if(textureInfo.storageType != StorageType::KEEP_PIXEL_BUFFER)
950 NotifyObservers(textureInfo, false);
952 else // if(textureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER) // image mask case
954 // Check if there was another texture waiting for this load to complete
955 // (e.g. if this was an image mask, and its load is on a different thread)
956 CheckForWaitingTexture(textureInfo);
961 void TextureManager::CheckForWaitingTexture(TextureManager::TextureInfo& maskTextureInfo)
963 // Search the cache, checking if any texture has this texture id as a
965 const std::size_t size = mTextureCacheManager.size();
967 const bool maskLoadSuccess = maskTextureInfo.loadState == LoadState::LOAD_FINISHED ? true : false;
969 // TODO : Refactorize here to not iterate whole cached image.
970 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
972 if(mTextureCacheManager[cacheIndex].maskTextureId == maskTextureInfo.textureId &&
973 mTextureCacheManager[cacheIndex].loadState == LoadState::WAITING_FOR_MASK)
975 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
979 // Send New Task to Thread
980 ApplyMask(textureInfo, maskTextureInfo.textureId);
984 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
985 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
986 UploadTexture(textureInfo.pixelBuffer, textureInfo);
987 NotifyObservers(textureInfo, true);
993 void TextureManager::ApplyMask(TextureManager::TextureInfo& textureInfo, const TextureManager::TextureId& maskTextureId)
995 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
996 if(maskCacheIndex != INVALID_CACHE_INDEX)
998 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
999 Devel::PixelBuffer pixelBuffer = textureInfo.pixelBuffer;
1000 textureInfo.pixelBuffer.Reset();
1002 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ApplyMask(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
1004 textureInfo.loadState = LoadState::MASK_APPLYING;
1005 auto& loadersContainer = (textureInfo.url.IsLocalResource() || textureInfo.url.IsBufferResource()) ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
1006 auto loadingHelperIt = loadersContainer.GetNext();
1007 auto premultiplyOnLoad = textureInfo.preMultiplyOnLoad ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
1008 DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
1009 loadingHelperIt->ApplyMask(textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask, premultiplyOnLoad);
1013 void TextureManager::UploadTexture(Devel::PixelBuffer& pixelBuffer, TextureManager::TextureInfo& textureInfo)
1015 if(textureInfo.loadState != LoadState::UPLOADED && textureInfo.useAtlas != UseAtlas::USE_ATLAS)
1017 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, " TextureManager::UploadTexture() New Texture for textureId:%d\n", textureInfo.textureId);
1019 // Check if this pixelBuffer is premultiplied
1020 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
1022 auto& renderingAddOn = RenderingAddOn::Get();
1023 if(renderingAddOn.IsValid())
1025 renderingAddOn.CreateGeometry(textureInfo.textureId, pixelBuffer);
1028 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight());
1030 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
1031 texture.Upload(pixelData);
1032 if(!textureInfo.textureSet)
1034 textureInfo.textureSet = TextureSet::New();
1036 textureInfo.textureSet.SetTexture(0u, texture);
1039 // Update the load state.
1040 // Note: This is regardless of success as we care about whether a
1041 // load attempt is in progress or not. If unsuccessful, a broken
1042 // image is still loaded.
1043 textureInfo.loadState = LoadState::UPLOADED;
1046 void TextureManager::NotifyObservers(TextureManager::TextureInfo& textureInfo, const bool& success)
1048 TextureId textureId = textureInfo.textureId;
1050 // If there is an observer: Notify the load is complete, whether successful or not,
1051 // and erase it from the list
1052 TextureInfo* info = &textureInfo;
1054 if(info->animatedImageLoading)
1056 // If loading failed, we don't need to get frameCount and frameInterval.
1059 info->frameCount = info->animatedImageLoading.GetImageCount();
1060 info->frameInterval = info->animatedImageLoading.GetFrameInterval(info->frameIndex);
1062 info->animatedImageLoading.Reset();
1065 mQueueLoadFlag = true;
1067 // Reverse observer list that we can pop_back the observer.
1068 std::reverse(info->observerList.Begin(), info->observerList.End());
1070 while(info->observerList.Count())
1072 TextureUploadObserver* observer = *(info->observerList.End() - 1u);
1074 // During LoadComplete() a Control ResourceReady() signal is emitted.
1075 // During that signal the app may add remove /add Textures (e.g. via
1077 // It is possible for observers to be removed from the observer list,
1078 // and it is also possible for the mTextureInfoContainer to be modified,
1079 // invalidating the reference to the textureInfo struct.
1080 // Texture load requests for the same URL are deferred until the end of this
1082 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::NotifyObservers() textureId:%d url:%s loadState:%s\n", textureId, info->url.GetUrl().c_str(), GET_LOAD_STATE_STRING(info->loadState));
1084 // It is possible for the observer to be deleted.
1085 // Disconnect and remove the observer first.
1086 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1088 info->observerList.Erase(info->observerList.End() - 1u);
1090 EmitLoadComplete(observer, *info, success);
1092 // Get the textureInfo from the container again as it may have been invalidated.
1093 TextureCacheIndex textureInfoIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1094 if(textureInfoIndex == INVALID_CACHE_INDEX)
1096 break; // texture has been removed - can stop.
1098 info = &mTextureCacheManager[textureInfoIndex];
1101 mQueueLoadFlag = false;
1103 ProcessRemoveQueue();
1105 if(info->storageType == StorageType::RETURN_PIXEL_BUFFER && info->observerList.Count() == 0)
1107 Remove(info->textureId, nullptr);
1111 void TextureManager::ObserverDestroyed(TextureUploadObserver* observer)
1113 const std::size_t size = mTextureCacheManager.size();
1114 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1116 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1117 for(TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin();
1118 j != textureInfo.observerList.End();)
1122 j = textureInfo.observerList.Erase(j);
1131 // Remove element from the LoadQueue
1132 for(auto&& element : mLoadQueue)
1134 if(element.mObserver == observer)
1136 element.mObserver = nullptr;
1141 Dali::Geometry TextureManager::GetRenderGeometry(const TextureManager::TextureId& textureId, std::uint32_t& frontElements, std::uint32_t& backElements)
1143 return RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().GetGeometry(textureId, frontElements, backElements) : Geometry();
1146 void TextureManager::EmitLoadComplete(TextureUploadObserver* observer, TextureManager::TextureInfo& textureInfo, const bool& success)
1148 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1150 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::PIXEL_BUFFER, textureInfo.pixelBuffer, textureInfo.url.GetUrl(), textureInfo.preMultiplied));
1152 else if(textureInfo.isAnimatedImageFormat)
1154 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::ANIMATED_IMAGE_TEXTURE, textureInfo.textureId, textureInfo.frameCount, textureInfo.frameInterval));
1158 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::TEXTURE, textureInfo.textureId, textureInfo.textureSet, (textureInfo.useAtlas == UseAtlas::USE_ATLAS) ? true : false, textureInfo.atlasRect, textureInfo.preMultiplied));
1162 } // namespace Internal
1164 } // namespace Toolkit