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 mLoadingQueueTextureId(INVALID_TEXTURE_ID),
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 ||
457 mLoadingQueueTextureId != INVALID_TEXTURE_ID);
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)
728 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
729 if(textureCacheIndex != INVALID_CACHE_INDEX)
731 TextureManager::TextureId maskTextureId = INVALID_TEXTURE_ID;
732 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
733 if(textureInfo.maskTextureId != INVALID_TEXTURE_ID)
735 maskTextureId = textureInfo.maskTextureId;
738 // the case that LoadingQueue is working.
739 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
741 // If textureId is not same, this observer need to delete when ProcessRemoveQueue() is called.
742 TextureUploadObserver* queueObserver = nullptr;
743 if(mLoadingQueueTextureId != textureId)
745 queueObserver = observer;
748 // Remove textureId after NotifyObserver finished
749 if(maskTextureId != INVALID_TEXTURE_ID)
751 if(textureInfo.loadState != LoadState::CANCELLED)
753 mRemoveQueue.PushBack(QueueElement(maskTextureId, nullptr));
756 mRemoveQueue.PushBack(QueueElement(textureId, queueObserver));
760 // Remove its observer
761 RemoveTextureObserver(textureInfo, observer);
763 // Remove maskTextureId in CacheManager
764 if(maskTextureId != INVALID_TEXTURE_ID)
766 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
767 if(maskCacheIndex != INVALID_CACHE_INDEX)
769 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
771 // Only Remove maskTexture when texture's loadState is not CANCELLED. because it is already deleted.
772 if(textureInfo.loadState != LoadState::CANCELLED)
774 mTextureCacheManager.RemoveCache(maskTextureInfo);
779 // Remove textureId in CacheManager
780 mTextureCacheManager.RemoveCache(textureInfo);
786 // Remove element from the LoadQueue
787 for(auto&& element : mLoadQueue)
789 if(element.mObserver == observer)
791 // Do not erase the item. We will clear it later in ProcessLoadQueue().
792 element.mObserver = nullptr;
800 void TextureManager::LoadImageSynchronously(
801 const VisualUrl& url,
802 const Dali::ImageDimensions& desiredSize,
803 const Dali::FittingMode::Type& fittingMode,
804 const Dali::SamplingMode::Type& samplingMode,
805 const bool& orientationCorrection,
806 const bool& loadYuvPlanes,
807 std::vector<Devel::PixelBuffer>& pixelBuffers)
809 Devel::PixelBuffer pixelBuffer;
810 if(url.IsBufferResource())
812 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
813 if(encodedImageBuffer)
815 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
822 Dali::LoadImagePlanesFromFile(url.GetUrl(), pixelBuffers, desiredSize, fittingMode, samplingMode, orientationCorrection);
826 pixelBuffer = Dali::LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
832 pixelBuffers.push_back(pixelBuffer);
836 void TextureManager::AddObserver(TextureManager::LifecycleObserver& observer)
838 // make sure an observer doesn't observe the same object twice
839 // otherwise it will get multiple calls to ObjectDestroyed()
840 DALI_ASSERT_DEBUG(mLifecycleObservers.End() == std::find(mLifecycleObservers.Begin(), mLifecycleObservers.End(), &observer));
841 mLifecycleObservers.PushBack(&observer);
844 void TextureManager::RemoveObserver(TextureManager::LifecycleObserver& observer)
846 // Find the observer...
847 auto endIter = mLifecycleObservers.End();
848 for(auto iter = mLifecycleObservers.Begin(); iter != endIter; ++iter)
850 if((*iter) == &observer)
852 mLifecycleObservers.Erase(iter);
856 DALI_ASSERT_DEBUG(endIter != mLifecycleObservers.End());
859 void TextureManager::LoadOrQueueTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
861 switch(textureInfo.loadState)
863 case LoadState::NOT_STARTED:
864 case LoadState::LOAD_FAILED:
866 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
868 QueueLoadTexture(textureInfo, observer);
872 LoadTexture(textureInfo, observer);
876 case LoadState::UPLOADED:
878 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
880 QueueLoadTexture(textureInfo, observer);
884 // The Texture has already loaded. The other observers have already been notified.
885 // We need to send a "late" loaded notification for this observer.
886 EmitLoadComplete(observer, textureInfo, true);
890 case LoadState::LOADING:
891 case LoadState::CANCELLED:
892 case LoadState::LOAD_FINISHED:
893 case LoadState::WAITING_FOR_MASK:
894 case LoadState::MASK_APPLYING:
895 case LoadState::MASK_APPLIED:
902 void TextureManager::QueueLoadTexture(const TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
904 const auto& textureId = textureInfo.textureId;
905 mLoadQueue.PushBack(QueueElement(textureId, observer));
907 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
910 void TextureManager::LoadTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
912 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::LoadTexture(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
914 textureInfo.loadState = LoadState::LOADING;
915 if(!textureInfo.loadSynchronously)
917 auto& loadersContainer = (textureInfo.url.IsLocalResource() || textureInfo.url.IsBufferResource()) ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
918 auto loadingHelperIt = loadersContainer.GetNext();
919 auto premultiplyOnLoad = (textureInfo.preMultiplyOnLoad && textureInfo.maskTextureId == INVALID_TEXTURE_ID) ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
920 DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
921 if(textureInfo.animatedImageLoading)
923 loadingHelperIt->LoadAnimatedImage(textureInfo.textureId, textureInfo.animatedImageLoading, textureInfo.frameIndex, premultiplyOnLoad);
927 loadingHelperIt->Load(textureInfo.textureId, textureInfo.url, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, textureInfo.orientationCorrection, premultiplyOnLoad, textureInfo.loadYuvPlanes);
930 ObserveTexture(textureInfo, observer);
933 void TextureManager::ProcessLoadQueue()
935 for(auto&& element : mLoadQueue)
937 if(!element.mObserver)
942 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(element.mTextureId);
943 if(cacheIndex != INVALID_CACHE_INDEX)
945 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
946 if((textureInfo.loadState == LoadState::UPLOADED) || (textureInfo.loadState == LoadState::LOAD_FINISHED && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER))
948 EmitLoadComplete(element.mObserver, textureInfo, true);
950 else if(textureInfo.loadState == LoadState::LOADING)
952 // Note : LOADING state texture cannot be queue.
953 // This case be occured when same texture id are queue in mLoadQueue.
954 ObserveTexture(textureInfo, element.mObserver);
958 LoadTexture(textureInfo, element.mObserver);
965 void TextureManager::ProcessRemoveQueue()
967 TextureCacheIndex textureCacheIndex = INVALID_CACHE_INDEX;
968 for(auto&& element : mRemoveQueue)
970 textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(element.mTextureId);
971 if(textureCacheIndex != INVALID_CACHE_INDEX)
973 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
974 RemoveTextureObserver(textureInfo, element.mObserver);
975 mTextureCacheManager.RemoveCache(textureInfo);
978 mRemoveQueue.Clear();
981 void TextureManager::ObserveTexture(TextureManager::TextureInfo& textureInfo,
982 TextureUploadObserver* observer)
984 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ObserveTexture(): url:%s observer:%p\n", textureInfo.url.GetUrl().c_str(), observer);
988 textureInfo.observerList.PushBack(observer);
989 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
993 void TextureManager::AsyncLoadComplete(const TextureManager::TextureId& textureId, std::vector<Devel::PixelBuffer>& pixelBuffers)
995 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
996 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::AsyncLoadComplete( textureId:%d CacheIndex:%d )\n", textureId, cacheIndex.GetIndex());
997 if(cacheIndex != INVALID_CACHE_INDEX)
999 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1001 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));
1003 if(textureInfo.loadState != LoadState::CANCELLED)
1005 // textureInfo can be invalidated after this call (as the mTextureInfoContainer may be modified)
1006 PostLoad(textureInfo, pixelBuffers);
1010 Remove(textureInfo.textureId, nullptr);
1015 void TextureManager::PostLoad(TextureManager::TextureInfo& textureInfo, std::vector<Devel::PixelBuffer>& pixelBuffers)
1017 // Was the load successful?
1018 if(!pixelBuffers.empty())
1020 if(pixelBuffers.size() == 1)
1022 Devel::PixelBuffer pixelBuffer = pixelBuffers[0];
1023 if(pixelBuffer && (pixelBuffer.GetWidth() != 0) && (pixelBuffer.GetHeight() != 0))
1025 // No atlas support for now
1026 textureInfo.useAtlas = UseAtlas::NO_ATLAS;
1027 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
1029 if(textureInfo.storageType == StorageType::UPLOAD_TO_TEXTURE)
1031 // If there is a mask texture ID associated with this texture, then apply the mask
1032 // if it's already loaded. If it hasn't, and the mask is still loading,
1033 // wait for the mask to finish loading.
1034 // note, If the texture is already uploaded synchronously during loading,
1035 // we don't need to apply mask.
1036 if(textureInfo.loadState != LoadState::UPLOADED &&
1037 textureInfo.maskTextureId != INVALID_TEXTURE_ID)
1039 if(textureInfo.loadState == LoadState::MASK_APPLYING)
1041 textureInfo.loadState = LoadState::MASK_APPLIED;
1042 UploadTextures(pixelBuffers, textureInfo);
1043 NotifyObservers(textureInfo, true);
1047 LoadState maskLoadState = mTextureCacheManager.GetTextureStateInternal(textureInfo.maskTextureId);
1048 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
1049 if(maskLoadState == LoadState::LOADING)
1051 textureInfo.loadState = LoadState::WAITING_FOR_MASK;
1053 else if(maskLoadState == LoadState::LOAD_FINISHED || maskLoadState == LoadState::UPLOADED)
1055 // Send New Task to Thread
1056 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureInfo.maskTextureId);
1057 if(maskCacheIndex != INVALID_CACHE_INDEX)
1059 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
1060 if(maskTextureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER)
1062 // Send New Task to Thread
1063 ApplyMask(textureInfo, textureInfo.maskTextureId);
1065 else if(maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1067 // Upload image texture. textureInfo.loadState will be UPLOADED.
1068 UploadTextures(pixelBuffers, textureInfo);
1069 if(maskTextureInfo.textureSet.GetTextureCount() > 0u)
1071 Texture maskTexture = maskTextureInfo.textureSet.GetTexture(0u);
1072 textureInfo.textureSet.SetTexture(1u, maskTexture);
1074 // notify mask texture set.
1075 NotifyObservers(textureInfo, true);
1079 else // maskLoadState == LoadState::LOAD_FAILED
1081 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1082 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1083 UploadTextures(pixelBuffers, textureInfo);
1084 NotifyObservers(textureInfo, true);
1090 UploadTextures(pixelBuffers, textureInfo);
1091 NotifyObservers(textureInfo, true);
1096 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
1097 textureInfo.loadState = LoadState::LOAD_FINISHED;
1099 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1101 NotifyObservers(textureInfo, true);
1103 else // for the StorageType::KEEP_PIXEL_BUFFER and StorageType::KEEP_TEXTURE
1105 // Check if there was another texture waiting for this load to complete
1106 // (e.g. if this was an image mask, and its load is on a different thread)
1107 CheckForWaitingTexture(textureInfo);
1115 // No atlas support for now
1116 textureInfo.useAtlas = UseAtlas::NO_ATLAS;
1117 textureInfo.preMultiplied = false;
1119 UploadTextures(pixelBuffers, textureInfo);
1120 NotifyObservers(textureInfo, true);
1125 textureInfo.loadState = LoadState::LOAD_FAILED;
1126 if(textureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER || textureInfo.storageType == StorageType::KEEP_TEXTURE)
1128 // Check if there was another texture waiting for this load to complete
1129 // (e.g. if this was an image mask, and its load is on a different thread)
1130 CheckForWaitingTexture(textureInfo);
1134 NotifyObservers(textureInfo, false);
1139 void TextureManager::CheckForWaitingTexture(TextureManager::TextureInfo& maskTextureInfo)
1141 if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED &&
1142 maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1144 // Upload mask texture. textureInfo.loadState will be UPLOADED.
1145 std::vector<Devel::PixelBuffer> pixelBuffers;
1146 pixelBuffers.push_back(maskTextureInfo.pixelBuffer);
1147 UploadTextures(pixelBuffers, maskTextureInfo);
1150 // Search the cache, checking if any texture has this texture id as a
1152 const std::size_t size = mTextureCacheManager.size();
1154 // TODO : Refactorize here to not iterate whole cached image.
1155 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1157 if(mTextureCacheManager[cacheIndex].maskTextureId == maskTextureInfo.textureId &&
1158 mTextureCacheManager[cacheIndex].loadState == LoadState::WAITING_FOR_MASK)
1160 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1162 if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED)
1164 if(maskTextureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER)
1166 // Send New Task to Thread
1167 ApplyMask(textureInfo, maskTextureInfo.textureId);
1170 else if(maskTextureInfo.loadState == LoadState::UPLOADED)
1172 if(maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1174 // Upload image texture. textureInfo.loadState will be UPLOADED.
1175 std::vector<Devel::PixelBuffer> pixelBuffers;
1176 pixelBuffers.push_back(textureInfo.pixelBuffer);
1177 UploadTextures(pixelBuffers, textureInfo);
1178 if(maskTextureInfo.textureSet.GetTextureCount() > 0u)
1180 Texture maskTexture = maskTextureInfo.textureSet.GetTexture(0u);
1181 textureInfo.textureSet.SetTexture(1u, maskTexture);
1183 // notify mask texture set.
1184 NotifyObservers(textureInfo, true);
1189 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1190 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1191 std::vector<Devel::PixelBuffer> pixelBuffers;
1192 pixelBuffers.push_back(textureInfo.pixelBuffer);
1193 UploadTextures(pixelBuffers, textureInfo);
1194 NotifyObservers(textureInfo, true);
1200 void TextureManager::ApplyMask(TextureManager::TextureInfo& textureInfo, const TextureManager::TextureId& maskTextureId)
1202 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
1203 if(maskCacheIndex != INVALID_CACHE_INDEX)
1205 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
1206 Devel::PixelBuffer pixelBuffer = textureInfo.pixelBuffer;
1207 textureInfo.pixelBuffer.Reset();
1209 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ApplyMask(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
1211 textureInfo.loadState = LoadState::MASK_APPLYING;
1212 auto& loadersContainer = (textureInfo.url.IsLocalResource() || textureInfo.url.IsBufferResource()) ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
1213 auto loadingHelperIt = loadersContainer.GetNext();
1214 auto premultiplyOnLoad = textureInfo.preMultiplyOnLoad ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
1215 DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
1216 loadingHelperIt->ApplyMask(textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask, premultiplyOnLoad);
1220 void TextureManager::UploadTextures(std::vector<Devel::PixelBuffer>& pixelBuffers, TextureManager::TextureInfo& textureInfo)
1222 if(!pixelBuffers.empty() && textureInfo.loadState != LoadState::UPLOADED && textureInfo.useAtlas != UseAtlas::USE_ATLAS)
1224 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, " TextureManager::UploadTextures() New Texture for textureId:%d\n", textureInfo.textureId);
1226 // Check if this pixelBuffer is premultiplied
1227 textureInfo.preMultiplied = pixelBuffers[0].IsAlphaPreMultiplied();
1229 auto& renderingAddOn = RenderingAddOn::Get();
1230 if(renderingAddOn.IsValid())
1232 renderingAddOn.CreateGeometry(textureInfo.textureId, pixelBuffers[0]);
1235 if(!textureInfo.textureSet)
1237 textureInfo.textureSet = TextureSet::New();
1241 for(auto&& pixelBuffer : pixelBuffers)
1243 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight());
1245 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
1246 texture.Upload(pixelData);
1247 textureInfo.textureSet.SetTexture(index++, texture);
1251 // Update the load state.
1252 // Note: This is regardless of success as we care about whether a
1253 // load attempt is in progress or not. If unsuccessful, a broken
1254 // image is still loaded.
1255 textureInfo.loadState = LoadState::UPLOADED;
1258 void TextureManager::NotifyObservers(TextureManager::TextureInfo& textureInfo, const bool& success)
1260 TextureId textureId = textureInfo.textureId;
1262 // If there is an observer: Notify the load is complete, whether successful or not,
1263 // and erase it from the list
1264 TextureInfo* info = &textureInfo;
1266 if(info->animatedImageLoading)
1268 // If loading failed, we don't need to get frameCount and frameInterval.
1271 info->frameCount = info->animatedImageLoading.GetImageCount();
1272 info->frameInterval = info->animatedImageLoading.GetFrameInterval(info->frameIndex);
1274 info->animatedImageLoading.Reset();
1277 mLoadingQueueTextureId = textureId;
1279 // Reverse observer list that we can pop_back the observer.
1280 std::reverse(info->observerList.Begin(), info->observerList.End());
1282 while(info->observerList.Count())
1284 TextureUploadObserver* observer = *(info->observerList.End() - 1u);
1286 // During LoadComplete() a Control ResourceReady() signal is emitted.
1287 // During that signal the app may add remove /add Textures (e.g. via
1289 // It is possible for observers to be removed from the observer list,
1290 // and it is also possible for the mTextureInfoContainer to be modified,
1291 // invalidating the reference to the textureInfo struct.
1292 // Texture load requests for the same URL are deferred until the end of this
1294 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));
1296 // It is possible for the observer to be deleted.
1297 // Disconnect and remove the observer first.
1298 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1300 info->observerList.Erase(info->observerList.End() - 1u);
1302 EmitLoadComplete(observer, *info, success);
1304 // Get the textureInfo from the container again as it may have been invalidated.
1305 TextureCacheIndex textureInfoIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1306 if(textureInfoIndex == INVALID_CACHE_INDEX)
1308 break; // texture has been removed - can stop.
1310 info = &mTextureCacheManager[textureInfoIndex];
1313 mLoadingQueueTextureId = INVALID_TEXTURE_ID;
1315 ProcessRemoveQueue();
1317 if(info->storageType == StorageType::RETURN_PIXEL_BUFFER && info->observerList.Count() == 0)
1319 Remove(info->textureId, nullptr);
1323 void TextureManager::ObserverDestroyed(TextureUploadObserver* observer)
1325 const std::size_t size = mTextureCacheManager.size();
1326 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1328 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1329 for(TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin();
1330 j != textureInfo.observerList.End();)
1334 j = textureInfo.observerList.Erase(j);
1343 // Remove element from the LoadQueue
1344 for(auto&& element : mLoadQueue)
1346 if(element.mObserver == observer)
1348 element.mObserver = nullptr;
1353 Dali::Geometry TextureManager::GetRenderGeometry(const TextureManager::TextureId& textureId, std::uint32_t& frontElements, std::uint32_t& backElements)
1355 return RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().GetGeometry(textureId, frontElements, backElements) : Geometry();
1358 void TextureManager::EmitLoadComplete(TextureUploadObserver* observer, TextureManager::TextureInfo& textureInfo, const bool& success)
1360 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1362 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::PIXEL_BUFFER, textureInfo.pixelBuffer, textureInfo.url.GetUrl(), textureInfo.preMultiplied));
1364 else if(textureInfo.isAnimatedImageFormat)
1366 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::ANIMATED_IMAGE_TEXTURE, textureInfo.textureId, textureInfo.frameCount, textureInfo.frameInterval));
1370 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::TEXTURE, textureInfo.textureId, textureInfo.textureSet, (textureInfo.useAtlas == UseAtlas::USE_ATLAS) ? true : false, textureInfo.atlasRect, textureInfo.preMultiplied));
1374 void TextureManager::RemoveTextureObserver(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
1376 // Remove its observer
1379 const auto iterEnd = textureInfo.observerList.End();
1380 const auto iter = std::find(textureInfo.observerList.Begin(), iterEnd, observer);
1383 // Disconnect and remove the observer.
1384 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1385 textureInfo.observerList.Erase(iter);
1390 } // namespace Internal
1392 } // namespace Toolkit