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";
42 constexpr auto LOAD_IMAGE_YUV_PLANES_ENV = "DALI_LOAD_IMAGE_YUV_PLANES";
44 size_t GetNumberOfThreads(const char* environmentVariable, size_t defaultValue)
46 using Dali::EnvironmentVariable::GetEnvironmentVariable;
47 auto numberString = GetEnvironmentVariable(environmentVariable);
48 auto numberOfThreads = numberString ? std::strtoul(numberString, nullptr, 10) : 0;
49 constexpr auto MAX_NUMBER_OF_THREADS = 100u;
50 DALI_ASSERT_DEBUG(numberOfThreads < MAX_NUMBER_OF_THREADS);
51 return (numberOfThreads > 0 && numberOfThreads < MAX_NUMBER_OF_THREADS) ? numberOfThreads : defaultValue;
54 size_t GetNumberOfLocalLoaderThreads()
56 return GetNumberOfThreads(NUMBER_OF_LOCAL_LOADER_THREADS_ENV, DEFAULT_NUMBER_OF_LOCAL_LOADER_THREADS);
59 size_t GetNumberOfRemoteLoaderThreads()
61 return GetNumberOfThreads(NUMBER_OF_REMOTE_LOADER_THREADS_ENV, DEFAULT_NUMBER_OF_REMOTE_LOADER_THREADS);
64 bool NeedToLoadYuvPlanes()
66 auto loadYuvPlanesString = Dali::EnvironmentVariable::GetEnvironmentVariable(LOAD_IMAGE_YUV_PLANES_ENV);
67 bool loadYuvPlanes = loadYuvPlanesString ? std::atoi(loadYuvPlanesString) : false;
79 // Define logfilter Internal namespace level so other files (e.g. texture-cache-manager.cpp) can also use this filter.
80 Debug::Filter* gTextureManagerLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TEXTURE_MANAGER");
83 #define GET_LOAD_STATE_STRING(loadState) \
84 loadState == TextureManagerType::LoadState::NOT_STARTED ? "NOT_STARTED" : \
85 loadState == TextureManagerType::LoadState::LOADING ? "LOADING" : \
86 loadState == TextureManagerType::LoadState::LOAD_FINISHED ? "LOAD_FINISHED" : \
87 loadState == TextureManagerType::LoadState::WAITING_FOR_MASK ? "WAITING_FOR_MASK" : \
88 loadState == TextureManagerType::LoadState::MASK_APPLYING ? "MASK_APPLYING" : \
89 loadState == TextureManagerType::LoadState::MASK_APPLIED ? "MASK_APPLIED" : \
90 loadState == TextureManagerType::LoadState::UPLOADED ? "UPLOADED" : \
91 loadState == TextureManagerType::LoadState::CANCELLED ? "CANCELLED" : \
92 loadState == TextureManagerType::LoadState::LOAD_FAILED ? "LOAD_FAILED" : \
99 const uint32_t DEFAULT_ATLAS_SIZE(1024u); ///< This size can fit 8 by 8 images of average size 128 * 128
100 const Vector4 FULL_ATLAS_RECT(0.0f, 0.0f, 1.0f, 1.0f); ///< UV Rectangle that covers the full Texture
102 void PreMultiply(Devel::PixelBuffer pixelBuffer, TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
104 if(Pixel::HasAlpha(pixelBuffer.GetPixelFormat()))
106 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
108 pixelBuffer.MultiplyColorByAlpha();
113 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
117 } // Anonymous namespace
119 TextureManager::MaskingData::MaskingData()
121 mAlphaMaskId(INVALID_TEXTURE_ID),
122 mContentScaleFactor(1.0f),
124 mPreappliedMasking(true),
125 mMaskImageLoadingFailed(false)
129 TextureManager::TextureManager()
130 : mTextureCacheManager(),
131 mAsyncLocalLoaders(GetNumberOfLocalLoaderThreads(), [&]() { return TextureAsyncLoadingHelper(*this); }),
132 mAsyncRemoteLoaders(GetNumberOfRemoteLoaderThreads(), [&]() { return TextureAsyncLoadingHelper(*this); }),
133 mLifecycleObservers(),
136 mQueueLoadFlag(false),
137 mLoadYuvPlanes(NeedToLoadYuvPlanes())
139 // Initialize the AddOn
140 RenderingAddOn::Get();
143 TextureManager::~TextureManager()
145 for(auto iter = mLifecycleObservers.Begin(), endIter = mLifecycleObservers.End(); iter != endIter; ++iter)
147 (*iter)->TextureManagerDestroyed();
151 TextureSet TextureManager::LoadAnimatedImageTexture(
152 const VisualUrl& url,
153 Dali::AnimatedImageLoading animatedImageLoading,
154 const uint32_t& frameIndex,
155 TextureManager::TextureId& textureId,
156 MaskingDataPointer& maskInfo,
157 const Dali::SamplingMode::Type& samplingMode,
158 const Dali::WrapMode::Type& wrapModeU,
159 const Dali::WrapMode::Type& wrapModeV,
160 const bool& synchronousLoading,
161 TextureUploadObserver* textureObserver,
162 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
164 TextureSet textureSet;
166 if(synchronousLoading)
168 Devel::PixelBuffer pixelBuffer;
169 if(animatedImageLoading)
171 pixelBuffer = animatedImageLoading.LoadFrame(frameIndex);
175 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous loading is failed\n");
180 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
182 Devel::PixelBuffer maskPixelBuffer = LoadImageFromFile(maskInfo->mAlphaMaskUrl.GetUrl(), ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true);
185 if(!maskInfo->mPreappliedMasking)
187 PixelData maskPixelData = Devel::PixelBuffer::Convert(maskPixelBuffer); // takes ownership of buffer
188 maskTexture = Texture::New(Dali::TextureType::TEXTURE_2D, maskPixelData.GetPixelFormat(), maskPixelData.GetWidth(), maskPixelData.GetHeight());
189 maskTexture.Upload(maskPixelData);
193 pixelBuffer.ApplyMask(maskPixelBuffer, maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
198 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous mask image loading is failed\n");
202 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
204 PreMultiply(pixelBuffer, preMultiplyOnLoad);
207 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
210 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight());
211 texture.Upload(pixelData);
212 textureSet = TextureSet::New();
213 textureSet.SetTexture(0u, texture);
216 textureSet.SetTexture(1u, maskTexture);
223 TextureId alphaMaskId = INVALID_TEXTURE_ID;
224 float contentScaleFactor = 1.0f;
225 bool cropToMask = false;
226 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
228 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, maskInfo->mPreappliedMasking ? StorageType::KEEP_PIXEL_BUFFER : StorageType::KEEP_TEXTURE);
229 alphaMaskId = maskInfo->mAlphaMaskId;
230 if(maskInfo->mPreappliedMasking)
232 contentScaleFactor = maskInfo->mContentScaleFactor;
233 cropToMask = maskInfo->mCropToMask;
237 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);
239 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
240 if(loadState == TextureManager::LoadState::UPLOADED)
242 // LoadComplete has already been called - keep the same texture set
243 textureSet = GetTextureSet(textureId);
249 Sampler sampler = Sampler::New();
250 sampler.SetWrapMode(wrapModeU, wrapModeV);
251 textureSet.SetSampler(0u, sampler);
257 Devel::PixelBuffer TextureManager::LoadPixelBuffer(
258 const VisualUrl& url,
259 const Dali::ImageDimensions& desiredSize,
260 const Dali::FittingMode::Type& fittingMode,
261 const Dali::SamplingMode::Type& samplingMode,
262 const bool& synchronousLoading,
263 TextureUploadObserver* textureObserver,
264 const bool& orientationCorrection,
265 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
267 Devel::PixelBuffer pixelBuffer;
268 if(synchronousLoading)
272 if(url.IsBufferResource())
274 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
275 if(encodedImageBuffer)
277 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
282 pixelBuffer = LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
284 if(pixelBuffer && preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
286 PreMultiply(pixelBuffer, preMultiplyOnLoad);
292 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);
298 TextureSet TextureManager::LoadTexture(
299 const VisualUrl& url,
300 const Dali::ImageDimensions& desiredSize,
301 const Dali::FittingMode::Type& fittingMode,
302 const Dali::SamplingMode::Type& samplingMode,
303 MaskingDataPointer& maskInfo,
304 const bool& synchronousLoading,
305 TextureManager::TextureId& textureId,
306 Vector4& textureRect,
307 Dali::ImageDimensions& textureRectSize,
308 bool& atlasingStatus,
310 const Dali::WrapMode::Type& wrapModeU,
311 const Dali::WrapMode::Type& wrapModeV,
312 TextureUploadObserver* textureObserver,
313 AtlasUploadObserver* atlasObserver,
314 ImageAtlasManagerPtr imageAtlasManager,
315 const bool& orientationCorrection,
316 const TextureManager::ReloadPolicy& reloadPolicy,
317 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
319 TextureSet textureSet;
321 loadingStatus = false;
322 textureRect = FULL_ATLAS_RECT;
324 if(VisualUrl::TEXTURE == url.GetProtocolType())
326 std::string location = url.GetLocation();
327 if(location.size() > 0u)
329 TextureId id = std::stoi(location);
330 textureSet = mTextureCacheManager.GetExternalTextureSet(id);
333 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
342 if(synchronousLoading && atlasingStatus)
344 const bool synchronousAtlasAvaliable = (desiredSize != ImageDimensions() || url.IsLocalResource()) ? imageAtlasManager->CheckAtlasAvailable(url, desiredSize)
346 if(synchronousAtlasAvaliable)
348 std::vector<Devel::PixelBuffer> pixelBuffers;
349 LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection, false, pixelBuffers);
351 if(!pixelBuffers.empty() && maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
353 std::vector<Devel::PixelBuffer> maskPixelBuffers;
354 LoadImageSynchronously(maskInfo->mAlphaMaskUrl, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true, false, maskPixelBuffers);
355 if(!maskPixelBuffers.empty())
357 pixelBuffers[0].ApplyMask(maskPixelBuffers[0], maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
362 if(!pixelBuffers.empty())
364 PreMultiply(pixelBuffers[0], preMultiplyOnLoad);
365 data = Devel::PixelBuffer::Convert(pixelBuffers[0]); // takes ownership of buffer
369 textureSet = imageAtlasManager->Add(textureRect, data);
372 textureRectSize.SetWidth(data.GetWidth());
373 textureRectSize.SetHeight(data.GetHeight());
378 DALI_LOG_ERROR("TextureManager::LoadTexture: Synchronous Texture loading with atlasing is failed.\n");
383 atlasingStatus = false;
390 loadingStatus = true;
391 // Atlas manager can chage desired size when it is set by 0,0.
392 // We should store into textureRectSize only if atlasing successed.
393 // So copy inputed desiredSize, and replace value into textureRectSize only if atlasing success.
394 Dali::ImageDimensions atlasDesiredSize = desiredSize;
397 if(url.IsBufferResource())
399 const EncodedImageBuffer& encodedImageBuffer = GetEncodedImageBuffer(url.GetUrl());
400 if(encodedImageBuffer)
402 textureSet = imageAtlasManager->Add(textureRect, encodedImageBuffer, desiredSize, fittingMode, true, atlasObserver);
407 textureSet = imageAtlasManager->Add(textureRect, url, atlasDesiredSize, fittingMode, true, atlasObserver);
410 if(!textureSet) // big image, no atlasing or atlasing failed
412 atlasingStatus = false;
414 TextureId alphaMaskId = INVALID_TEXTURE_ID;
415 float contentScaleFactor = 1.0f;
416 bool cropToMask = false;
417 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
419 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, maskInfo->mPreappliedMasking ? StorageType::KEEP_PIXEL_BUFFER : StorageType::KEEP_TEXTURE, synchronousLoading);
420 alphaMaskId = maskInfo->mAlphaMaskId;
421 if(maskInfo && maskInfo->mPreappliedMasking)
423 contentScaleFactor = maskInfo->mContentScaleFactor;
424 cropToMask = maskInfo->mCropToMask;
428 textureId = RequestLoad(
438 orientationCorrection,
443 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
444 if(loadState == TextureManager::LoadState::UPLOADED)
446 // LoadComplete has already been called - keep the same texture set
447 textureSet = mTextureCacheManager.GetTextureSet(textureId);
450 // If we are loading the texture, or waiting for the ready signal handler to complete, inform
451 // caller that they need to wait.
452 loadingStatus = (loadState == TextureManager::LoadState::LOADING ||
453 loadState == TextureManager::LoadState::WAITING_FOR_MASK ||
454 loadState == TextureManager::LoadState::MASK_APPLYING ||
455 loadState == TextureManager::LoadState::MASK_APPLIED ||
456 loadState == TextureManager::LoadState::NOT_STARTED ||
461 textureRectSize = atlasDesiredSize;
466 if(!atlasingStatus && textureSet)
468 Sampler sampler = Sampler::New();
469 sampler.SetWrapMode(wrapModeU, wrapModeV);
470 textureSet.SetSampler(0u, sampler);
473 if(synchronousLoading)
475 loadingStatus = false;
481 TextureManager::TextureId TextureManager::RequestLoad(
482 const VisualUrl& url,
483 const ImageDimensions& desiredSize,
484 const Dali::FittingMode::Type& fittingMode,
485 const Dali::SamplingMode::Type& samplingMode,
486 const UseAtlas& useAtlas,
487 TextureUploadObserver* observer,
488 const bool& orientationCorrection,
489 const TextureManager::ReloadPolicy& reloadPolicy,
490 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
491 const bool& synchronousLoading)
493 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);
496 TextureManager::TextureId TextureManager::RequestLoad(
497 const VisualUrl& url,
498 const TextureManager::TextureId& maskTextureId,
499 const float& contentScale,
500 const Dali::ImageDimensions& desiredSize,
501 const Dali::FittingMode::Type& fittingMode,
502 const Dali::SamplingMode::Type& samplingMode,
503 const TextureManager::UseAtlas& useAtlas,
504 const bool& cropToMask,
505 TextureUploadObserver* observer,
506 const bool& orientationCorrection,
507 const TextureManager::ReloadPolicy& reloadPolicy,
508 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
509 const bool& synchronousLoading)
511 return RequestLoadInternal(url, maskTextureId, contentScale, desiredSize, fittingMode, samplingMode, useAtlas, cropToMask, StorageType::UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
514 TextureManager::TextureId TextureManager::RequestMaskLoad(
515 const VisualUrl& maskUrl,
516 StorageType storageType,
517 const bool& synchronousLoading)
519 // Use the normal load procedure to get the alpha mask.
520 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
521 return RequestLoadInternal(maskUrl, INVALID_TEXTURE_ID, 1.0f, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, UseAtlas::NO_ATLAS, false, storageType, NULL, true, TextureManager::ReloadPolicy::CACHED, preMultiply, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
524 TextureManager::TextureId TextureManager::RequestLoadInternal(
525 const VisualUrl& url,
526 const TextureManager::TextureId& maskTextureId,
527 const float& contentScale,
528 const Dali::ImageDimensions& desiredSize,
529 const Dali::FittingMode::Type& fittingMode,
530 const Dali::SamplingMode::Type& samplingMode,
531 const TextureManager::UseAtlas& useAtlas,
532 const bool& cropToMask,
533 const TextureManager::StorageType& storageType,
534 TextureUploadObserver* observer,
535 const bool& orientationCorrection,
536 const TextureManager::ReloadPolicy& reloadPolicy,
537 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
538 Dali::AnimatedImageLoading animatedImageLoading,
539 const std::uint32_t& frameIndex,
540 const bool& synchronousLoading)
542 TextureHash textureHash = INITIAL_HASH_NUMBER;
543 TextureCacheIndex cacheIndex = INVALID_CACHE_INDEX;
544 bool loadYuvPlanes = (mLoadYuvPlanes && maskTextureId == INVALID_TEXTURE_ID && storageType == StorageType::UPLOAD_TO_TEXTURE);
546 if(storageType != StorageType::RETURN_PIXEL_BUFFER)
548 textureHash = mTextureCacheManager.GenerateHash(url, desiredSize, fittingMode, samplingMode, useAtlas, maskTextureId, cropToMask, frameIndex);
550 // Look up the texture by hash. Note: The extra parameters are used in case of a hash collision.
551 cacheIndex = mTextureCacheManager.FindCachedTexture(textureHash, url, desiredSize, fittingMode, samplingMode, useAtlas, storageType, maskTextureId, cropToMask, preMultiplyOnLoad, (animatedImageLoading) ? true : false, frameIndex);
554 TextureManager::TextureId textureId = INVALID_TEXTURE_ID;
555 // Check if the requested Texture exists in the cache.
556 if(cacheIndex != INVALID_CACHE_INDEX)
558 if(TextureManager::ReloadPolicy::CACHED == reloadPolicy)
560 // Mark this texture being used by another client resource. Forced reload would replace the current texture
561 // without the need for incrementing the reference count.
562 ++(mTextureCacheManager[cacheIndex].referenceCount);
564 textureId = mTextureCacheManager[cacheIndex].textureId;
566 // Update preMultiplyOnLoad value. It should be changed according to preMultiplied value of the cached info.
567 preMultiplyOnLoad = mTextureCacheManager[cacheIndex].preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
569 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);
572 if(textureId == INVALID_TEXTURE_ID) // There was no caching, or caching not required
574 textureId = mTextureCacheManager.GenerateTextureId();
576 bool preMultiply = (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
578 // Cache new texutre, and get cacheIndex.
579 cacheIndex = mTextureCacheManager.AppendCache(TextureInfo(textureId, maskTextureId, url, desiredSize, contentScale, fittingMode, samplingMode, false, cropToMask, useAtlas, textureHash, orientationCorrection, preMultiply, animatedImageLoading, frameIndex, loadYuvPlanes));
581 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::RequestLoad( url=%s observer=%p ) New texture, cacheIndex:%d, textureId=%d, frameindex=%d premultiply=%d\n", url.GetUrl().c_str(), observer, cacheIndex.GetIndex(), textureId, frameIndex, preMultiply);
584 // The below code path is common whether we are using the cache or not.
585 // The textureInfoIndex now refers to either a pre-existing cached TextureInfo,
586 // or a new TextureInfo just created.
587 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
588 textureInfo.maskTextureId = maskTextureId;
589 textureInfo.storageType = storageType;
590 textureInfo.orientationCorrection = orientationCorrection;
592 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureInfo loadState:%s\n", GET_LOAD_STATE_STRING(textureInfo.loadState));
594 // Force reloading of texture by setting loadState unless already loading or cancelled.
595 if(TextureManager::ReloadPolicy::FORCED == reloadPolicy &&
596 TextureManager::LoadState::LOADING != textureInfo.loadState &&
597 TextureManager::LoadState::WAITING_FOR_MASK != textureInfo.loadState &&
598 TextureManager::LoadState::MASK_APPLYING != textureInfo.loadState &&
599 TextureManager::LoadState::MASK_APPLIED != textureInfo.loadState &&
600 TextureManager::LoadState::CANCELLED != textureInfo.loadState)
602 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);
604 textureInfo.loadState = TextureManager::LoadState::NOT_STARTED;
607 if(!synchronousLoading)
609 // Check if we should add the observer.
610 // Only do this if we have not loaded yet and it will not have loaded by the end of this method.
611 switch(textureInfo.loadState)
613 case TextureManager::LoadState::LOAD_FAILED: // Failed notifies observer which then stops observing.
614 case TextureManager::LoadState::NOT_STARTED:
616 LoadOrQueueTexture(textureInfo, observer); // If called inside NotifyObservers, queues until afterwards
619 case TextureManager::LoadState::LOADING:
620 case TextureManager::LoadState::WAITING_FOR_MASK:
621 case TextureManager::LoadState::MASK_APPLYING:
622 case TextureManager::LoadState::MASK_APPLIED:
624 ObserveTexture(textureInfo, observer);
627 case TextureManager::LoadState::UPLOADED:
631 LoadOrQueueTexture(textureInfo, observer);
635 case TextureManager::LoadState::CANCELLED:
637 // A cancelled texture hasn't finished loading yet. Treat as a loading texture
638 // (it's ref count has already been incremented, above)
639 textureInfo.loadState = TextureManager::LoadState::LOADING;
640 ObserveTexture(textureInfo, observer);
643 case TextureManager::LoadState::LOAD_FINISHED:
645 // Loading has already completed.
646 if(observer && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
648 LoadOrQueueTexture(textureInfo, observer);
656 // If the image is already finished to load, use cached texture.
657 // We don't need to consider Observer because this is synchronous loading.
658 if(!(textureInfo.loadState == TextureManager::LoadState::UPLOADED ||
659 textureInfo.loadState == TextureManager::LoadState::LOAD_FINISHED))
661 std::vector<Devel::PixelBuffer> pixelBuffers;
662 LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection, loadYuvPlanes, pixelBuffers);
664 if(pixelBuffers.empty())
666 // If pixelBuffer loading is failed in synchronously, call Remove() method.
667 Remove(textureId, nullptr);
668 return INVALID_TEXTURE_ID;
671 if(storageType == StorageType::KEEP_PIXEL_BUFFER) // For the mask image loading.
673 textureInfo.pixelBuffer = pixelBuffers[0]; // Store the pixel data
674 textureInfo.loadState = LoadState::LOAD_FINISHED;
676 else // For the image loading.
679 if(maskTextureId != INVALID_TEXTURE_ID)
681 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
682 if(maskCacheIndex != INVALID_CACHE_INDEX)
684 if(mTextureCacheManager[maskCacheIndex].storageType == StorageType::KEEP_TEXTURE)
686 TextureSet maskTextures = mTextureCacheManager[maskCacheIndex].textureSet;
687 if(maskTextures && maskTextures.GetTextureCount())
689 maskTexture = maskTextures.GetTexture(0u);
692 else if(mTextureCacheManager[maskCacheIndex].storageType == StorageType::KEEP_PIXEL_BUFFER)
694 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
697 pixelBuffers[0].ApplyMask(maskPixelBuffer, contentScale, cropToMask);
701 DALI_LOG_ERROR("Mask image cached invalid pixel buffer!\n");
707 DALI_LOG_ERROR("Mask image is not stored in cache.\n");
710 PreMultiply(pixelBuffers[0], preMultiplyOnLoad);
713 UploadTextures(pixelBuffers, textureInfo);
714 if(maskTexture && textureInfo.textureSet)
716 textureInfo.textureSet.SetTexture(1u, maskTexture);
724 void TextureManager::Remove(const TextureManager::TextureId& textureId, TextureUploadObserver* observer)
726 if(textureId != INVALID_TEXTURE_ID)
730 // Remove textureId after NotifyObserver finished
731 mRemoveQueue.PushBack(textureId);
735 // Remove textureId in CacheManager.
736 mTextureCacheManager.RemoveCache(textureId);
741 // Remove element from the LoadQueue
742 for(auto&& element : mLoadQueue)
744 if(element.mObserver == observer)
746 // Do not erase the item. We will clear it later in ProcessLoadQueue().
747 element.mObserver = nullptr;
755 void TextureManager::LoadImageSynchronously(
756 const VisualUrl& url,
757 const Dali::ImageDimensions& desiredSize,
758 const Dali::FittingMode::Type& fittingMode,
759 const Dali::SamplingMode::Type& samplingMode,
760 const bool& orientationCorrection,
761 const bool& loadYuvPlanes,
762 std::vector<Devel::PixelBuffer>& pixelBuffers)
764 Devel::PixelBuffer pixelBuffer;
765 if(url.IsBufferResource())
767 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
768 if(encodedImageBuffer)
770 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
777 Dali::LoadImagePlanesFromFile(url.GetUrl(), pixelBuffers, desiredSize, fittingMode, samplingMode, orientationCorrection);
781 pixelBuffer = Dali::LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
787 pixelBuffers.push_back(pixelBuffer);
791 void TextureManager::AddObserver(TextureManager::LifecycleObserver& observer)
793 // make sure an observer doesn't observe the same object twice
794 // otherwise it will get multiple calls to ObjectDestroyed()
795 DALI_ASSERT_DEBUG(mLifecycleObservers.End() == std::find(mLifecycleObservers.Begin(), mLifecycleObservers.End(), &observer));
796 mLifecycleObservers.PushBack(&observer);
799 void TextureManager::RemoveObserver(TextureManager::LifecycleObserver& observer)
801 // Find the observer...
802 auto endIter = mLifecycleObservers.End();
803 for(auto iter = mLifecycleObservers.Begin(); iter != endIter; ++iter)
805 if((*iter) == &observer)
807 mLifecycleObservers.Erase(iter);
811 DALI_ASSERT_DEBUG(endIter != mLifecycleObservers.End());
814 void TextureManager::LoadOrQueueTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
816 switch(textureInfo.loadState)
818 case LoadState::NOT_STARTED:
819 case LoadState::LOAD_FAILED:
823 QueueLoadTexture(textureInfo, observer);
827 LoadTexture(textureInfo, observer);
831 case LoadState::UPLOADED:
835 QueueLoadTexture(textureInfo, observer);
839 // The Texture has already loaded. The other observers have already been notified.
840 // We need to send a "late" loaded notification for this observer.
841 EmitLoadComplete(observer, textureInfo, true);
845 case LoadState::LOADING:
846 case LoadState::CANCELLED:
847 case LoadState::LOAD_FINISHED:
848 case LoadState::WAITING_FOR_MASK:
849 case LoadState::MASK_APPLYING:
850 case LoadState::MASK_APPLIED:
857 void TextureManager::QueueLoadTexture(const TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
859 const auto& textureId = textureInfo.textureId;
860 mLoadQueue.PushBack(LoadQueueElement(textureId, observer));
862 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
865 void TextureManager::LoadTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
867 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::LoadTexture(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
869 textureInfo.loadState = LoadState::LOADING;
870 if(!textureInfo.loadSynchronously)
872 auto& loadersContainer = (textureInfo.url.IsLocalResource() || textureInfo.url.IsBufferResource()) ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
873 auto loadingHelperIt = loadersContainer.GetNext();
874 auto premultiplyOnLoad = (textureInfo.preMultiplyOnLoad && textureInfo.maskTextureId == INVALID_TEXTURE_ID) ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
875 DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
876 if(textureInfo.animatedImageLoading)
878 loadingHelperIt->LoadAnimatedImage(textureInfo.textureId, textureInfo.animatedImageLoading, textureInfo.frameIndex, premultiplyOnLoad);
882 loadingHelperIt->Load(textureInfo.textureId, textureInfo.url, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, textureInfo.orientationCorrection, premultiplyOnLoad, textureInfo.loadYuvPlanes);
885 ObserveTexture(textureInfo, observer);
888 void TextureManager::ProcessLoadQueue()
890 for(auto&& element : mLoadQueue)
892 if(!element.mObserver)
897 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(element.mTextureId);
898 if(cacheIndex != INVALID_CACHE_INDEX)
900 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
901 if((textureInfo.loadState == LoadState::UPLOADED) || (textureInfo.loadState == LoadState::LOAD_FINISHED && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER))
903 EmitLoadComplete(element.mObserver, textureInfo, true);
905 else if(textureInfo.loadState == LoadState::LOADING)
907 // Note : LOADING state texture cannot be queue.
908 // This case be occured when same texture id are queue in mLoadQueue.
909 ObserveTexture(textureInfo, element.mObserver);
913 LoadTexture(textureInfo, element.mObserver);
920 void TextureManager::ProcessRemoveQueue()
922 for(const auto& textureId : mRemoveQueue)
924 mTextureCacheManager.RemoveCache(textureId);
926 mRemoveQueue.Clear();
929 void TextureManager::ObserveTexture(TextureManager::TextureInfo& textureInfo,
930 TextureUploadObserver* observer)
932 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ObserveTexture(): url:%s observer:%p\n", textureInfo.url.GetUrl().c_str(), observer);
936 textureInfo.observerList.PushBack(observer);
937 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
941 void TextureManager::AsyncLoadComplete(const TextureManager::TextureId& textureId, std::vector<Devel::PixelBuffer>& pixelBuffers)
943 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
944 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::AsyncLoadComplete( textureId:%d CacheIndex:%d )\n", textureId, cacheIndex.GetIndex());
945 if(cacheIndex != INVALID_CACHE_INDEX)
947 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
949 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));
951 if(textureInfo.loadState != LoadState::CANCELLED)
953 // textureInfo can be invalidated after this call (as the mTextureInfoContainer may be modified)
954 PostLoad(textureInfo, pixelBuffers);
958 Remove(textureInfo.textureId, nullptr);
963 void TextureManager::PostLoad(TextureManager::TextureInfo& textureInfo, std::vector<Devel::PixelBuffer>& pixelBuffers)
965 // Was the load successful?
966 if(!pixelBuffers.empty())
968 if(pixelBuffers.size() == 1)
970 Devel::PixelBuffer pixelBuffer = pixelBuffers[0];
971 if(pixelBuffer && (pixelBuffer.GetWidth() != 0) && (pixelBuffer.GetHeight() != 0))
973 // No atlas support for now
974 textureInfo.useAtlas = UseAtlas::NO_ATLAS;
975 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
977 if(textureInfo.storageType == StorageType::UPLOAD_TO_TEXTURE)
979 // If there is a mask texture ID associated with this texture, then apply the mask
980 // if it's already loaded. If it hasn't, and the mask is still loading,
981 // wait for the mask to finish loading.
982 // note, If the texture is already uploaded synchronously during loading,
983 // we don't need to apply mask.
984 if(textureInfo.loadState != LoadState::UPLOADED &&
985 textureInfo.maskTextureId != INVALID_TEXTURE_ID)
987 if(textureInfo.loadState == LoadState::MASK_APPLYING)
989 textureInfo.loadState = LoadState::MASK_APPLIED;
990 UploadTextures(pixelBuffers, textureInfo);
991 NotifyObservers(textureInfo, true);
995 LoadState maskLoadState = mTextureCacheManager.GetTextureStateInternal(textureInfo.maskTextureId);
996 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
997 if(maskLoadState == LoadState::LOADING)
999 textureInfo.loadState = LoadState::WAITING_FOR_MASK;
1001 else if(maskLoadState == LoadState::LOAD_FINISHED || maskLoadState == LoadState::UPLOADED)
1003 // Send New Task to Thread
1004 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureInfo.maskTextureId);
1005 if(maskCacheIndex != INVALID_CACHE_INDEX)
1007 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
1008 if(maskTextureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER)
1010 // Send New Task to Thread
1011 ApplyMask(textureInfo, textureInfo.maskTextureId);
1013 else if(maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1015 // Upload image texture. textureInfo.loadState will be UPLOADED.
1016 UploadTextures(pixelBuffers, textureInfo);
1017 if(maskTextureInfo.textureSet.GetTextureCount() > 0u)
1019 Texture maskTexture = maskTextureInfo.textureSet.GetTexture(0u);
1020 textureInfo.textureSet.SetTexture(1u, maskTexture);
1022 // notify mask texture set.
1023 NotifyObservers(textureInfo, true);
1027 else // maskLoadState == LoadState::LOAD_FAILED
1029 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1030 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1031 UploadTextures(pixelBuffers, textureInfo);
1032 NotifyObservers(textureInfo, true);
1038 UploadTextures(pixelBuffers, textureInfo);
1039 NotifyObservers(textureInfo, true);
1044 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
1045 textureInfo.loadState = LoadState::LOAD_FINISHED;
1047 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1049 NotifyObservers(textureInfo, true);
1051 else // for the StorageType::KEEP_PIXEL_BUFFER and StorageType::KEEP_TEXTURE
1053 // Check if there was another texture waiting for this load to complete
1054 // (e.g. if this was an image mask, and its load is on a different thread)
1055 CheckForWaitingTexture(textureInfo);
1063 // No atlas support for now
1064 textureInfo.useAtlas = UseAtlas::NO_ATLAS;
1065 textureInfo.preMultiplied = false;
1067 UploadTextures(pixelBuffers, textureInfo);
1068 NotifyObservers(textureInfo, true);
1073 textureInfo.loadState = LoadState::LOAD_FAILED;
1074 if(textureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER || textureInfo.storageType == StorageType::KEEP_TEXTURE)
1076 // Check if there was another texture waiting for this load to complete
1077 // (e.g. if this was an image mask, and its load is on a different thread)
1078 CheckForWaitingTexture(textureInfo);
1082 NotifyObservers(textureInfo, false);
1087 void TextureManager::CheckForWaitingTexture(TextureManager::TextureInfo& maskTextureInfo)
1089 if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED &&
1090 maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1092 // Upload mask texture. textureInfo.loadState will be UPLOADED.
1093 std::vector<Devel::PixelBuffer> pixelBuffers;
1094 pixelBuffers.push_back(maskTextureInfo.pixelBuffer);
1095 UploadTextures(pixelBuffers, maskTextureInfo);
1098 // Search the cache, checking if any texture has this texture id as a
1100 const std::size_t size = mTextureCacheManager.size();
1102 // TODO : Refactorize here to not iterate whole cached image.
1103 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1105 if(mTextureCacheManager[cacheIndex].maskTextureId == maskTextureInfo.textureId &&
1106 mTextureCacheManager[cacheIndex].loadState == LoadState::WAITING_FOR_MASK)
1108 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1110 if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED)
1112 if(maskTextureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER)
1114 // Send New Task to Thread
1115 ApplyMask(textureInfo, maskTextureInfo.textureId);
1118 else if(maskTextureInfo.loadState == LoadState::UPLOADED)
1120 if(maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1122 // Upload image texture. textureInfo.loadState will be UPLOADED.
1123 std::vector<Devel::PixelBuffer> pixelBuffers;
1124 pixelBuffers.push_back(textureInfo.pixelBuffer);
1125 UploadTextures(pixelBuffers, textureInfo);
1126 if(maskTextureInfo.textureSet.GetTextureCount() > 0u)
1128 Texture maskTexture = maskTextureInfo.textureSet.GetTexture(0u);
1129 textureInfo.textureSet.SetTexture(1u, maskTexture);
1131 // notify mask texture set.
1132 NotifyObservers(textureInfo, true);
1137 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1138 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1139 std::vector<Devel::PixelBuffer> pixelBuffers;
1140 pixelBuffers.push_back(textureInfo.pixelBuffer);
1141 UploadTextures(pixelBuffers, textureInfo);
1142 NotifyObservers(textureInfo, true);
1148 void TextureManager::ApplyMask(TextureManager::TextureInfo& textureInfo, const TextureManager::TextureId& maskTextureId)
1150 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
1151 if(maskCacheIndex != INVALID_CACHE_INDEX)
1153 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
1154 Devel::PixelBuffer pixelBuffer = textureInfo.pixelBuffer;
1155 textureInfo.pixelBuffer.Reset();
1157 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ApplyMask(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
1159 textureInfo.loadState = LoadState::MASK_APPLYING;
1160 auto& loadersContainer = (textureInfo.url.IsLocalResource() || textureInfo.url.IsBufferResource()) ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
1161 auto loadingHelperIt = loadersContainer.GetNext();
1162 auto premultiplyOnLoad = textureInfo.preMultiplyOnLoad ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
1163 DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
1164 loadingHelperIt->ApplyMask(textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask, premultiplyOnLoad);
1168 void TextureManager::UploadTextures(std::vector<Devel::PixelBuffer>& pixelBuffers, TextureManager::TextureInfo& textureInfo)
1170 if(!pixelBuffers.empty() && textureInfo.loadState != LoadState::UPLOADED && textureInfo.useAtlas != UseAtlas::USE_ATLAS)
1172 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, " TextureManager::UploadTextures() New Texture for textureId:%d\n", textureInfo.textureId);
1174 // Check if this pixelBuffer is premultiplied
1175 textureInfo.preMultiplied = pixelBuffers[0].IsAlphaPreMultiplied();
1177 auto& renderingAddOn = RenderingAddOn::Get();
1178 if(renderingAddOn.IsValid())
1180 renderingAddOn.CreateGeometry(textureInfo.textureId, pixelBuffers[0]);
1183 if(!textureInfo.textureSet)
1185 textureInfo.textureSet = TextureSet::New();
1189 for(auto&& pixelBuffer : pixelBuffers)
1191 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight());
1193 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
1194 texture.Upload(pixelData);
1195 textureInfo.textureSet.SetTexture(index++, texture);
1199 // Update the load state.
1200 // Note: This is regardless of success as we care about whether a
1201 // load attempt is in progress or not. If unsuccessful, a broken
1202 // image is still loaded.
1203 textureInfo.loadState = LoadState::UPLOADED;
1206 void TextureManager::NotifyObservers(TextureManager::TextureInfo& textureInfo, const bool& success)
1208 TextureId textureId = textureInfo.textureId;
1210 // If there is an observer: Notify the load is complete, whether successful or not,
1211 // and erase it from the list
1212 TextureInfo* info = &textureInfo;
1214 if(info->animatedImageLoading)
1216 // If loading failed, we don't need to get frameCount and frameInterval.
1219 info->frameCount = info->animatedImageLoading.GetImageCount();
1220 info->frameInterval = info->animatedImageLoading.GetFrameInterval(info->frameIndex);
1222 info->animatedImageLoading.Reset();
1225 mQueueLoadFlag = true;
1227 // Reverse observer list that we can pop_back the observer.
1228 std::reverse(info->observerList.Begin(), info->observerList.End());
1230 while(info->observerList.Count())
1232 TextureUploadObserver* observer = *(info->observerList.End() - 1u);
1234 // During LoadComplete() a Control ResourceReady() signal is emitted.
1235 // During that signal the app may add remove /add Textures (e.g. via
1237 // It is possible for observers to be removed from the observer list,
1238 // and it is also possible for the mTextureInfoContainer to be modified,
1239 // invalidating the reference to the textureInfo struct.
1240 // Texture load requests for the same URL are deferred until the end of this
1242 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));
1244 // It is possible for the observer to be deleted.
1245 // Disconnect and remove the observer first.
1246 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1248 info->observerList.Erase(info->observerList.End() - 1u);
1250 EmitLoadComplete(observer, *info, success);
1252 // Get the textureInfo from the container again as it may have been invalidated.
1253 TextureCacheIndex textureInfoIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1254 if(textureInfoIndex == INVALID_CACHE_INDEX)
1256 break; // texture has been removed - can stop.
1258 info = &mTextureCacheManager[textureInfoIndex];
1261 mQueueLoadFlag = false;
1263 ProcessRemoveQueue();
1265 if(info->storageType == StorageType::RETURN_PIXEL_BUFFER && info->observerList.Count() == 0)
1267 Remove(info->textureId, nullptr);
1271 void TextureManager::ObserverDestroyed(TextureUploadObserver* observer)
1273 const std::size_t size = mTextureCacheManager.size();
1274 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1276 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1277 for(TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin();
1278 j != textureInfo.observerList.End();)
1282 j = textureInfo.observerList.Erase(j);
1291 // Remove element from the LoadQueue
1292 for(auto&& element : mLoadQueue)
1294 if(element.mObserver == observer)
1296 element.mObserver = nullptr;
1301 Dali::Geometry TextureManager::GetRenderGeometry(const TextureManager::TextureId& textureId, std::uint32_t& frontElements, std::uint32_t& backElements)
1303 return RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().GetGeometry(textureId, frontElements, backElements) : Geometry();
1306 void TextureManager::EmitLoadComplete(TextureUploadObserver* observer, TextureManager::TextureInfo& textureInfo, const bool& success)
1308 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1310 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::PIXEL_BUFFER, textureInfo.pixelBuffer, textureInfo.url.GetUrl(), textureInfo.preMultiplied));
1312 else if(textureInfo.isAnimatedImageFormat)
1314 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::ANIMATED_IMAGE_TEXTURE, textureInfo.textureId, textureInfo.frameCount, textureInfo.frameInterval));
1318 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::TEXTURE, textureInfo.textureId, textureInfo.textureSet, (textureInfo.useAtlas == UseAtlas::USE_ATLAS) ? true : false, textureInfo.atlasRect, textureInfo.preMultiplied));
1322 } // namespace Internal
1324 } // namespace Toolkit