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 const VisualUrl& url,
143 Dali::AnimatedImageLoading animatedImageLoading,
144 const uint32_t& frameIndex,
145 TextureManager::TextureId& textureId,
146 MaskingDataPointer& maskInfo,
147 const Dali::SamplingMode::Type& samplingMode,
148 const Dali::WrapMode::Type& wrapModeU,
149 const Dali::WrapMode::Type& wrapModeV,
150 const bool& synchronousLoading,
151 TextureUploadObserver* textureObserver,
152 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
154 TextureSet textureSet;
156 if(synchronousLoading)
158 Devel::PixelBuffer pixelBuffer;
159 if(animatedImageLoading)
161 pixelBuffer = animatedImageLoading.LoadFrame(frameIndex);
165 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous loading is failed\n");
169 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
171 Devel::PixelBuffer maskPixelBuffer = LoadImageFromFile(maskInfo->mAlphaMaskUrl.GetUrl(), ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true);
174 pixelBuffer.ApplyMask(maskPixelBuffer, maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
178 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous mask image loading is failed\n");
182 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
184 PreMultiply(pixelBuffer, preMultiplyOnLoad);
187 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
190 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight());
191 texture.Upload(pixelData);
192 textureSet = TextureSet::New();
193 textureSet.SetTexture(0u, texture);
199 TextureId alphaMaskId = INVALID_TEXTURE_ID;
200 float contentScaleFactor = 1.0f;
201 bool cropToMask = false;
202 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
204 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl);
205 alphaMaskId = maskInfo->mAlphaMaskId;
206 contentScaleFactor = maskInfo->mContentScaleFactor;
207 cropToMask = maskInfo->mCropToMask;
210 textureId = RequestLoadInternal(url, alphaMaskId, contentScaleFactor, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, UseAtlas::NO_ATLAS, cropToMask, StorageType::UPLOAD_TO_TEXTURE, textureObserver, true, TextureManager::ReloadPolicy::CACHED, preMultiplyOnLoad, animatedImageLoading, frameIndex, false);
212 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
213 if(loadState == TextureManager::LoadState::UPLOADED)
215 // LoadComplete has already been called - keep the same texture set
216 textureSet = GetTextureSet(textureId);
222 Sampler sampler = Sampler::New();
223 sampler.SetWrapMode(wrapModeU, wrapModeV);
224 textureSet.SetSampler(0u, sampler);
230 Devel::PixelBuffer TextureManager::LoadPixelBuffer(
231 const VisualUrl& url,
232 const Dali::ImageDimensions& desiredSize,
233 const Dali::FittingMode::Type& fittingMode,
234 const Dali::SamplingMode::Type& samplingMode,
235 const bool& synchronousLoading,
236 TextureUploadObserver* textureObserver,
237 const bool& orientationCorrection,
238 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
240 Devel::PixelBuffer pixelBuffer;
241 if(synchronousLoading)
245 if(url.IsBufferResource())
247 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
248 if(encodedImageBuffer)
250 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
255 pixelBuffer = LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
257 if(pixelBuffer && preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
259 PreMultiply(pixelBuffer, preMultiplyOnLoad);
265 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);
271 TextureSet TextureManager::LoadTexture(
272 const VisualUrl& url,
273 const Dali::ImageDimensions& desiredSize,
274 const Dali::FittingMode::Type& fittingMode,
275 const Dali::SamplingMode::Type& samplingMode,
276 MaskingDataPointer& maskInfo,
277 const bool& synchronousLoading,
278 TextureManager::TextureId& textureId,
279 Vector4& textureRect,
280 Dali::ImageDimensions& textureRectSize,
281 bool& atlasingStatus,
283 const Dali::WrapMode::Type& wrapModeU,
284 const Dali::WrapMode::Type& wrapModeV,
285 TextureUploadObserver* textureObserver,
286 AtlasUploadObserver* atlasObserver,
287 ImageAtlasManagerPtr imageAtlasManager,
288 const bool& orientationCorrection,
289 const TextureManager::ReloadPolicy& reloadPolicy,
290 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
292 TextureSet textureSet;
294 loadingStatus = false;
295 textureRect = FULL_ATLAS_RECT;
297 if(VisualUrl::TEXTURE == url.GetProtocolType())
299 std::string location = url.GetLocation();
300 if(location.size() > 0u)
302 TextureId id = std::stoi(location);
303 textureSet = mTextureCacheManager.GetExternalTextureSet(id);
306 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
315 if(synchronousLoading && atlasingStatus)
317 const bool synchronousAtlasAvaliable = (desiredSize != ImageDimensions() || url.IsLocalResource()) ? imageAtlasManager->CheckAtlasAvailable(url, desiredSize)
319 if(synchronousAtlasAvaliable)
321 Devel::PixelBuffer pixelBuffer = LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection);
323 if(pixelBuffer && maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
325 Devel::PixelBuffer maskPixelBuffer = LoadImageSynchronously(maskInfo->mAlphaMaskUrl, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true);
328 pixelBuffer.ApplyMask(maskPixelBuffer, maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
335 PreMultiply(pixelBuffer, preMultiplyOnLoad);
336 data = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
340 textureSet = imageAtlasManager->Add(textureRect, data);
343 textureRectSize.SetWidth(data.GetWidth());
344 textureRectSize.SetHeight(data.GetHeight());
349 DALI_LOG_ERROR("TextureManager::LoadTexture: Synchronous Texture loading with atlasing is failed.\n");
354 atlasingStatus = false;
361 loadingStatus = true;
362 // Atlas manager can chage desired size when it is set by 0,0.
363 // We should store into textureRectSize only if atlasing successed.
364 // So copy inputed desiredSize, and replace value into textureRectSize only if atlasing success.
365 Dali::ImageDimensions atlasDesiredSize = desiredSize;
368 if(url.IsBufferResource())
370 const EncodedImageBuffer& encodedImageBuffer = GetEncodedImageBuffer(url.GetUrl());
371 if(encodedImageBuffer)
373 textureSet = imageAtlasManager->Add(textureRect, encodedImageBuffer, desiredSize, fittingMode, true, atlasObserver);
378 textureSet = imageAtlasManager->Add(textureRect, url, atlasDesiredSize, fittingMode, true, atlasObserver);
381 if(!textureSet) // big image, no atlasing or atlasing failed
383 atlasingStatus = false;
384 if(!maskInfo || !maskInfo->mAlphaMaskUrl.IsValid())
386 textureId = RequestLoad(
393 orientationCorrection,
400 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, synchronousLoading);
401 textureId = RequestLoad(
403 maskInfo->mAlphaMaskId,
404 maskInfo->mContentScaleFactor,
409 maskInfo->mCropToMask,
411 orientationCorrection,
417 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
418 if(loadState == TextureManager::LoadState::UPLOADED)
420 // LoadComplete has already been called - keep the same texture set
421 textureSet = mTextureCacheManager.GetTextureSet(textureId);
424 // If we are loading the texture, or waiting for the ready signal handler to complete, inform
425 // caller that they need to wait.
426 loadingStatus = (loadState == TextureManager::LoadState::LOADING ||
427 loadState == TextureManager::LoadState::WAITING_FOR_MASK ||
428 loadState == TextureManager::LoadState::MASK_APPLYING ||
429 loadState == TextureManager::LoadState::MASK_APPLIED ||
430 loadState == TextureManager::LoadState::NOT_STARTED ||
435 textureRectSize = atlasDesiredSize;
440 if(!atlasingStatus && textureSet)
442 Sampler sampler = Sampler::New();
443 sampler.SetWrapMode(wrapModeU, wrapModeV);
444 textureSet.SetSampler(0u, sampler);
447 if(synchronousLoading)
449 loadingStatus = false;
455 TextureManager::TextureId TextureManager::RequestLoad(
456 const VisualUrl& url,
457 const ImageDimensions& desiredSize,
458 const Dali::FittingMode::Type& fittingMode,
459 const Dali::SamplingMode::Type& samplingMode,
460 const UseAtlas& useAtlas,
461 TextureUploadObserver* observer,
462 const bool& orientationCorrection,
463 const TextureManager::ReloadPolicy& reloadPolicy,
464 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
465 const bool& synchronousLoading)
467 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);
470 TextureManager::TextureId TextureManager::RequestLoad(
471 const VisualUrl& url,
472 const TextureManager::TextureId& maskTextureId,
473 const float& contentScale,
474 const Dali::ImageDimensions& desiredSize,
475 const Dali::FittingMode::Type& fittingMode,
476 const Dali::SamplingMode::Type& samplingMode,
477 const TextureManager::UseAtlas& useAtlas,
478 const bool& cropToMask,
479 TextureUploadObserver* observer,
480 const bool& orientationCorrection,
481 const TextureManager::ReloadPolicy& reloadPolicy,
482 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
483 const bool& synchronousLoading)
485 return RequestLoadInternal(url, maskTextureId, contentScale, desiredSize, fittingMode, samplingMode, useAtlas, cropToMask, StorageType::UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
488 TextureManager::TextureId TextureManager::RequestMaskLoad(
489 const VisualUrl& maskUrl,
490 const bool& synchronousLoading)
492 // Use the normal load procedure to get the alpha mask.
493 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
494 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);
497 TextureManager::TextureId TextureManager::RequestLoadInternal(
498 const VisualUrl& url,
499 const TextureManager::TextureId& maskTextureId,
500 const float& contentScale,
501 const Dali::ImageDimensions& desiredSize,
502 const Dali::FittingMode::Type& fittingMode,
503 const Dali::SamplingMode::Type& samplingMode,
504 const TextureManager::UseAtlas& useAtlas,
505 const bool& cropToMask,
506 const TextureManager::StorageType& storageType,
507 TextureUploadObserver* observer,
508 const bool& orientationCorrection,
509 const TextureManager::ReloadPolicy& reloadPolicy,
510 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
511 Dali::AnimatedImageLoading animatedImageLoading,
512 const std::uint32_t& frameIndex,
513 const bool& synchronousLoading)
515 TextureHash textureHash = INITIAL_HASH_NUMBER;
516 TextureCacheIndex cacheIndex = INVALID_CACHE_INDEX;
517 if(storageType != StorageType::RETURN_PIXEL_BUFFER && frameIndex == 0)
519 textureHash = mTextureCacheManager.GenerateHash(url, desiredSize, fittingMode, samplingMode, useAtlas, maskTextureId, cropToMask, frameIndex);
521 // Look up the texture by hash. Note: The extra parameters are used in case of a hash collision.
522 cacheIndex = mTextureCacheManager.FindCachedTexture(textureHash, url, desiredSize, fittingMode, samplingMode, useAtlas, maskTextureId, cropToMask, preMultiplyOnLoad, (animatedImageLoading) ? true : false, frameIndex);
525 TextureManager::TextureId textureId = INVALID_TEXTURE_ID;
526 // Check if the requested Texture exists in the cache.
527 if(cacheIndex != INVALID_CACHE_INDEX)
529 if(TextureManager::ReloadPolicy::CACHED == reloadPolicy)
531 // Mark this texture being used by another client resource. Forced reload would replace the current texture
532 // without the need for incrementing the reference count.
533 ++(mTextureCacheManager[cacheIndex].referenceCount);
535 textureId = mTextureCacheManager[cacheIndex].textureId;
537 // Update preMultiplyOnLoad value. It should be changed according to preMultiplied value of the cached info.
538 preMultiplyOnLoad = mTextureCacheManager[cacheIndex].preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
540 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);
543 if(textureId == INVALID_TEXTURE_ID) // There was no caching, or caching not required
545 textureId = mTextureCacheManager.GenerateTextureId();
547 bool preMultiply = (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
549 // Cache new texutre, and get cacheIndex.
550 cacheIndex = mTextureCacheManager.AppendCache(TextureInfo(textureId, maskTextureId, url, desiredSize, contentScale, fittingMode, samplingMode, false, cropToMask, useAtlas, textureHash, orientationCorrection, preMultiply, animatedImageLoading, frameIndex));
552 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);
555 // The below code path is common whether we are using the cache or not.
556 // The textureInfoIndex now refers to either a pre-existing cached TextureInfo,
557 // or a new TextureInfo just created.
558 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
559 textureInfo.maskTextureId = maskTextureId;
560 textureInfo.storageType = storageType;
561 textureInfo.orientationCorrection = orientationCorrection;
563 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureInfo loadState:%s\n", GET_LOAD_STATE_STRING(textureInfo.loadState));
565 // Force reloading of texture by setting loadState unless already loading or cancelled.
566 if(TextureManager::ReloadPolicy::FORCED == reloadPolicy &&
567 TextureManager::LoadState::LOADING != textureInfo.loadState &&
568 TextureManager::LoadState::WAITING_FOR_MASK != textureInfo.loadState &&
569 TextureManager::LoadState::MASK_APPLYING != textureInfo.loadState &&
570 TextureManager::LoadState::MASK_APPLIED != textureInfo.loadState &&
571 TextureManager::LoadState::CANCELLED != textureInfo.loadState)
573 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);
575 textureInfo.loadState = TextureManager::LoadState::NOT_STARTED;
578 if(!synchronousLoading)
580 // Check if we should add the observer.
581 // Only do this if we have not loaded yet and it will not have loaded by the end of this method.
582 switch(textureInfo.loadState)
584 case TextureManager::LoadState::LOAD_FAILED: // Failed notifies observer which then stops observing.
585 case TextureManager::LoadState::NOT_STARTED:
587 LoadOrQueueTexture(textureInfo, observer); // If called inside NotifyObservers, queues until afterwards
590 case TextureManager::LoadState::LOADING:
591 case TextureManager::LoadState::WAITING_FOR_MASK:
592 case TextureManager::LoadState::MASK_APPLYING:
593 case TextureManager::LoadState::MASK_APPLIED:
595 ObserveTexture(textureInfo, observer);
598 case TextureManager::LoadState::UPLOADED:
602 LoadOrQueueTexture(textureInfo, observer);
606 case TextureManager::LoadState::CANCELLED:
608 // A cancelled texture hasn't finished loading yet. Treat as a loading texture
609 // (it's ref count has already been incremented, above)
610 textureInfo.loadState = TextureManager::LoadState::LOADING;
611 ObserveTexture(textureInfo, observer);
614 case TextureManager::LoadState::LOAD_FINISHED:
616 // Loading has already completed.
617 if(observer && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
619 LoadOrQueueTexture(textureInfo, observer);
627 // If the image is already finished to load, use cached texture.
628 // We don't need to consider Observer because this is synchronous loading.
629 if(!(textureInfo.loadState == TextureManager::LoadState::UPLOADED ||
630 textureInfo.loadState == TextureManager::LoadState::LOAD_FINISHED))
632 Devel::PixelBuffer pixelBuffer = LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection);
636 // If pixelBuffer loading is failed in synchronously, call Remove() method.
637 Remove(textureId, nullptr);
638 return INVALID_TEXTURE_ID;
641 if(storageType == StorageType::KEEP_PIXEL_BUFFER) // For the mask image loading.
643 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
644 textureInfo.loadState = LoadState::LOAD_FINISHED;
646 else // For the image loading.
648 if(maskTextureId != INVALID_TEXTURE_ID)
650 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
651 if(maskCacheIndex != INVALID_CACHE_INDEX)
653 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
656 pixelBuffer.ApplyMask(maskPixelBuffer, contentScale, cropToMask);
660 DALI_LOG_ERROR("Mask image cached invalid pixel buffer!\n");
665 DALI_LOG_ERROR("Mask image is not stored in cache.\n");
668 PreMultiply(pixelBuffer, preMultiplyOnLoad);
671 UploadTexture(pixelBuffer, textureInfo);
678 void TextureManager::Remove(const TextureManager::TextureId& textureId, TextureUploadObserver* observer)
680 if(textureId != INVALID_TEXTURE_ID)
684 // Remove textureId after NotifyObserver finished
685 mRemoveQueue.PushBack(textureId);
689 // Remove textureId in CacheManager.
690 mTextureCacheManager.RemoveCache(textureId);
695 // Remove element from the LoadQueue
696 for(auto&& element : mLoadQueue)
698 if(element.mObserver == observer)
700 // Do not erase the item. We will clear it later in ProcessLoadQueue().
701 element.mObserver = nullptr;
709 Devel::PixelBuffer TextureManager::LoadImageSynchronously(
710 const VisualUrl& url,
711 const Dali::ImageDimensions& desiredSize,
712 const Dali::FittingMode::Type& fittingMode,
713 const Dali::SamplingMode::Type& samplingMode,
714 const bool& orientationCorrection)
716 Devel::PixelBuffer pixelBuffer;
717 if(url.IsBufferResource())
719 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
720 if(encodedImageBuffer)
722 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
727 pixelBuffer = LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
732 void TextureManager::AddObserver(TextureManager::LifecycleObserver& observer)
734 // make sure an observer doesn't observe the same object twice
735 // otherwise it will get multiple calls to ObjectDestroyed()
736 DALI_ASSERT_DEBUG(mLifecycleObservers.End() == std::find(mLifecycleObservers.Begin(), mLifecycleObservers.End(), &observer));
737 mLifecycleObservers.PushBack(&observer);
740 void TextureManager::RemoveObserver(TextureManager::LifecycleObserver& observer)
742 // Find the observer...
743 auto endIter = mLifecycleObservers.End();
744 for(auto iter = mLifecycleObservers.Begin(); iter != endIter; ++iter)
746 if((*iter) == &observer)
748 mLifecycleObservers.Erase(iter);
752 DALI_ASSERT_DEBUG(endIter != mLifecycleObservers.End());
755 void TextureManager::LoadOrQueueTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
757 switch(textureInfo.loadState)
759 case LoadState::NOT_STARTED:
760 case LoadState::LOAD_FAILED:
764 QueueLoadTexture(textureInfo, observer);
768 LoadTexture(textureInfo, observer);
772 case LoadState::UPLOADED:
776 QueueLoadTexture(textureInfo, observer);
780 // The Texture has already loaded. The other observers have already been notified.
781 // We need to send a "late" loaded notification for this observer.
782 EmitLoadComplete(observer, textureInfo, true);
786 case LoadState::LOADING:
787 case LoadState::CANCELLED:
788 case LoadState::LOAD_FINISHED:
789 case LoadState::WAITING_FOR_MASK:
790 case LoadState::MASK_APPLYING:
791 case LoadState::MASK_APPLIED:
798 void TextureManager::QueueLoadTexture(const TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
800 const auto& textureId = textureInfo.textureId;
801 mLoadQueue.PushBack(LoadQueueElement(textureId, observer));
803 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
806 void TextureManager::LoadTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
808 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::LoadTexture(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
810 textureInfo.loadState = LoadState::LOADING;
811 if(!textureInfo.loadSynchronously)
813 auto& loadersContainer = (textureInfo.url.IsLocalResource() || textureInfo.url.IsBufferResource()) ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
814 auto loadingHelperIt = loadersContainer.GetNext();
815 auto premultiplyOnLoad = (textureInfo.preMultiplyOnLoad && textureInfo.maskTextureId == INVALID_TEXTURE_ID) ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
816 DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
817 if(textureInfo.animatedImageLoading)
819 loadingHelperIt->LoadAnimatedImage(textureInfo.textureId, textureInfo.animatedImageLoading, textureInfo.frameIndex, premultiplyOnLoad);
823 loadingHelperIt->Load(textureInfo.textureId, textureInfo.url, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, textureInfo.orientationCorrection, premultiplyOnLoad);
826 ObserveTexture(textureInfo, observer);
829 void TextureManager::ProcessLoadQueue()
831 for(auto&& element : mLoadQueue)
833 if(!element.mObserver)
838 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(element.mTextureId);
839 if(cacheIndex != INVALID_CACHE_INDEX)
841 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
842 if((textureInfo.loadState == LoadState::UPLOADED) || (textureInfo.loadState == LoadState::LOAD_FINISHED && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER))
844 EmitLoadComplete(element.mObserver, textureInfo, true);
848 LoadTexture(textureInfo, element.mObserver);
855 void TextureManager::ProcessRemoveQueue()
857 for(const auto& textureId : mRemoveQueue)
859 mTextureCacheManager.RemoveCache(textureId);
861 mRemoveQueue.Clear();
864 void TextureManager::ObserveTexture(TextureManager::TextureInfo& textureInfo,
865 TextureUploadObserver* observer)
867 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ObserveTexture(): url:%s observer:%p\n", textureInfo.url.GetUrl().c_str(), observer);
871 textureInfo.observerList.PushBack(observer);
872 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
876 void TextureManager::AsyncLoadComplete(const TextureManager::TextureId& textureId, Devel::PixelBuffer pixelBuffer)
878 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
879 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::AsyncLoadComplete( textureId:%d CacheIndex:%d )\n", textureId, cacheIndex.GetIndex());
880 if(cacheIndex != INVALID_CACHE_INDEX)
882 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
884 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));
886 if(textureInfo.loadState != LoadState::CANCELLED)
888 // textureInfo can be invalidated after this call (as the mTextureInfoContainer may be modified)
889 PostLoad(textureInfo, pixelBuffer);
893 Remove(textureInfo.textureId, nullptr);
898 void TextureManager::PostLoad(TextureManager::TextureInfo& textureInfo, Devel::PixelBuffer& pixelBuffer)
900 // Was the load successful?
901 if(pixelBuffer && (pixelBuffer.GetWidth() != 0) && (pixelBuffer.GetHeight() != 0))
903 // No atlas support for now
904 textureInfo.useAtlas = UseAtlas::NO_ATLAS;
905 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
907 if(textureInfo.storageType == StorageType::UPLOAD_TO_TEXTURE)
909 // If there is a mask texture ID associated with this texture, then apply the mask
910 // if it's already loaded. If it hasn't, and the mask is still loading,
911 // wait for the mask to finish loading.
912 // note, If the texture is already uploaded synchronously during loading,
913 // we don't need to apply mask.
914 if(textureInfo.loadState != LoadState::UPLOADED &&
915 textureInfo.maskTextureId != INVALID_TEXTURE_ID)
917 if(textureInfo.loadState == LoadState::MASK_APPLYING)
919 textureInfo.loadState = LoadState::MASK_APPLIED;
920 UploadTexture(pixelBuffer, textureInfo);
921 NotifyObservers(textureInfo, true);
925 LoadState maskLoadState = mTextureCacheManager.GetTextureStateInternal(textureInfo.maskTextureId);
926 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
927 if(maskLoadState == LoadState::LOADING)
929 textureInfo.loadState = LoadState::WAITING_FOR_MASK;
931 else if(maskLoadState == LoadState::LOAD_FINISHED)
933 // Send New Task to Thread
934 ApplyMask(textureInfo, textureInfo.maskTextureId);
936 else // maskLoadState == LoadState::LOAD_FAILED
938 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
939 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
940 UploadTexture(pixelBuffer, textureInfo);
941 NotifyObservers(textureInfo, true);
947 UploadTexture(pixelBuffer, textureInfo);
948 NotifyObservers(textureInfo, true);
953 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
954 textureInfo.loadState = LoadState::LOAD_FINISHED;
956 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
958 NotifyObservers(textureInfo, true);
962 // Check if there was another texture waiting for this load to complete
963 // (e.g. if this was an image mask, and its load is on a different thread)
964 CheckForWaitingTexture(textureInfo);
970 textureInfo.loadState = LoadState::LOAD_FAILED;
971 if(textureInfo.storageType != StorageType::KEEP_PIXEL_BUFFER)
973 NotifyObservers(textureInfo, false);
975 else // if(textureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER) // image mask case
977 // Check if there was another texture waiting for this load to complete
978 // (e.g. if this was an image mask, and its load is on a different thread)
979 CheckForWaitingTexture(textureInfo);
984 void TextureManager::CheckForWaitingTexture(TextureManager::TextureInfo& maskTextureInfo)
986 // Search the cache, checking if any texture has this texture id as a
988 const std::size_t size = mTextureCacheManager.size();
990 const bool maskLoadSuccess = maskTextureInfo.loadState == LoadState::LOAD_FINISHED ? true : false;
992 // TODO : Refactorize here to not iterate whole cached image.
993 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
995 if(mTextureCacheManager[cacheIndex].maskTextureId == maskTextureInfo.textureId &&
996 mTextureCacheManager[cacheIndex].loadState == LoadState::WAITING_FOR_MASK)
998 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1002 // Send New Task to Thread
1003 ApplyMask(textureInfo, maskTextureInfo.textureId);
1007 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1008 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1009 UploadTexture(textureInfo.pixelBuffer, textureInfo);
1010 NotifyObservers(textureInfo, true);
1016 void TextureManager::ApplyMask(TextureManager::TextureInfo& textureInfo, const TextureManager::TextureId& maskTextureId)
1018 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
1019 if(maskCacheIndex != INVALID_CACHE_INDEX)
1021 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
1022 Devel::PixelBuffer pixelBuffer = textureInfo.pixelBuffer;
1023 textureInfo.pixelBuffer.Reset();
1025 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ApplyMask(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
1027 textureInfo.loadState = LoadState::MASK_APPLYING;
1028 auto& loadersContainer = (textureInfo.url.IsLocalResource() || textureInfo.url.IsBufferResource()) ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
1029 auto loadingHelperIt = loadersContainer.GetNext();
1030 auto premultiplyOnLoad = textureInfo.preMultiplyOnLoad ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
1031 DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
1032 loadingHelperIt->ApplyMask(textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask, premultiplyOnLoad);
1036 void TextureManager::UploadTexture(Devel::PixelBuffer& pixelBuffer, TextureManager::TextureInfo& textureInfo)
1038 if(textureInfo.loadState != LoadState::UPLOADED && textureInfo.useAtlas != UseAtlas::USE_ATLAS)
1040 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, " TextureManager::UploadTexture() New Texture for textureId:%d\n", textureInfo.textureId);
1042 // Check if this pixelBuffer is premultiplied
1043 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
1045 auto& renderingAddOn = RenderingAddOn::Get();
1046 if(renderingAddOn.IsValid())
1048 renderingAddOn.CreateGeometry(textureInfo.textureId, pixelBuffer);
1051 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight());
1053 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
1054 texture.Upload(pixelData);
1055 if(!textureInfo.textureSet)
1057 textureInfo.textureSet = TextureSet::New();
1059 textureInfo.textureSet.SetTexture(0u, texture);
1062 // Update the load state.
1063 // Note: This is regardless of success as we care about whether a
1064 // load attempt is in progress or not. If unsuccessful, a broken
1065 // image is still loaded.
1066 textureInfo.loadState = LoadState::UPLOADED;
1069 void TextureManager::NotifyObservers(TextureManager::TextureInfo& textureInfo, const bool& success)
1071 TextureId textureId = textureInfo.textureId;
1073 // If there is an observer: Notify the load is complete, whether successful or not,
1074 // and erase it from the list
1075 TextureInfo* info = &textureInfo;
1077 if(info->animatedImageLoading)
1079 // If loading failed, we don't need to get frameCount and frameInterval.
1082 info->frameCount = info->animatedImageLoading.GetImageCount();
1083 info->frameInterval = info->animatedImageLoading.GetFrameInterval(info->frameIndex);
1085 info->animatedImageLoading.Reset();
1088 mQueueLoadFlag = true;
1090 // Reverse observer list that we can pop_back the observer.
1091 std::reverse(info->observerList.Begin(), info->observerList.End());
1093 while(info->observerList.Count())
1095 TextureUploadObserver* observer = *(info->observerList.End() - 1u);
1097 // During LoadComplete() a Control ResourceReady() signal is emitted.
1098 // During that signal the app may add remove /add Textures (e.g. via
1100 // It is possible for observers to be removed from the observer list,
1101 // and it is also possible for the mTextureInfoContainer to be modified,
1102 // invalidating the reference to the textureInfo struct.
1103 // Texture load requests for the same URL are deferred until the end of this
1105 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));
1107 // It is possible for the observer to be deleted.
1108 // Disconnect and remove the observer first.
1109 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1111 info->observerList.Erase(info->observerList.End() - 1u);
1113 EmitLoadComplete(observer, *info, success);
1115 // Get the textureInfo from the container again as it may have been invalidated.
1116 TextureCacheIndex textureInfoIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1117 if(textureInfoIndex == INVALID_CACHE_INDEX)
1119 break; // texture has been removed - can stop.
1121 info = &mTextureCacheManager[textureInfoIndex];
1124 mQueueLoadFlag = false;
1126 ProcessRemoveQueue();
1128 if(info->storageType == StorageType::RETURN_PIXEL_BUFFER && info->observerList.Count() == 0)
1130 Remove(info->textureId, nullptr);
1134 void TextureManager::ObserverDestroyed(TextureUploadObserver* observer)
1136 const std::size_t size = mTextureCacheManager.size();
1137 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1139 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1140 for(TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin();
1141 j != textureInfo.observerList.End();)
1145 j = textureInfo.observerList.Erase(j);
1154 // Remove element from the LoadQueue
1155 for(auto&& element : mLoadQueue)
1157 if(element.mObserver == observer)
1159 element.mObserver = nullptr;
1164 Dali::Geometry TextureManager::GetRenderGeometry(const TextureManager::TextureId& textureId, std::uint32_t& frontElements, std::uint32_t& backElements)
1166 return RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().GetGeometry(textureId, frontElements, backElements) : Geometry();
1169 void TextureManager::EmitLoadComplete(TextureUploadObserver* observer, TextureManager::TextureInfo& textureInfo, const bool& success)
1171 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1173 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::PIXEL_BUFFER, textureInfo.pixelBuffer, textureInfo.url.GetUrl(), textureInfo.preMultiplied));
1175 else if(textureInfo.isAnimatedImageFormat)
1177 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::ANIMATED_IMAGE_TEXTURE, textureInfo.textureId, textureInfo.frameCount, textureInfo.frameInterval));
1181 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::TEXTURE, textureInfo.textureId, textureInfo.textureSet, (textureInfo.useAtlas == UseAtlas::USE_ATLAS) ? true : false, textureInfo.atlasRect, textureInfo.preMultiplied));
1185 } // namespace Internal
1187 } // namespace Toolkit