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 TEXTURE_INDEX = 0u; ///< The Index for texture
41 constexpr auto MASK_TEXTURE_INDEX = 1u; ///< The Index for mask texture
43 constexpr auto NUMBER_OF_LOCAL_LOADER_THREADS_ENV = "DALI_TEXTURE_LOCAL_THREADS";
44 constexpr auto NUMBER_OF_REMOTE_LOADER_THREADS_ENV = "DALI_TEXTURE_REMOTE_THREADS";
45 constexpr auto LOAD_IMAGE_YUV_PLANES_ENV = "DALI_LOAD_IMAGE_YUV_PLANES";
47 size_t GetNumberOfThreads(const char* environmentVariable, size_t defaultValue)
49 using Dali::EnvironmentVariable::GetEnvironmentVariable;
50 auto numberString = GetEnvironmentVariable(environmentVariable);
51 auto numberOfThreads = numberString ? std::strtoul(numberString, nullptr, 10) : 0;
52 constexpr auto MAX_NUMBER_OF_THREADS = 100u;
53 DALI_ASSERT_DEBUG(numberOfThreads < MAX_NUMBER_OF_THREADS);
54 return (numberOfThreads > 0 && numberOfThreads < MAX_NUMBER_OF_THREADS) ? numberOfThreads : defaultValue;
57 size_t GetNumberOfLocalLoaderThreads()
59 return GetNumberOfThreads(NUMBER_OF_LOCAL_LOADER_THREADS_ENV, DEFAULT_NUMBER_OF_LOCAL_LOADER_THREADS);
62 size_t GetNumberOfRemoteLoaderThreads()
64 return GetNumberOfThreads(NUMBER_OF_REMOTE_LOADER_THREADS_ENV, DEFAULT_NUMBER_OF_REMOTE_LOADER_THREADS);
67 bool NeedToLoadYuvPlanes()
69 auto loadYuvPlanesString = Dali::EnvironmentVariable::GetEnvironmentVariable(LOAD_IMAGE_YUV_PLANES_ENV);
70 bool loadYuvPlanes = loadYuvPlanesString ? std::atoi(loadYuvPlanesString) : false;
82 // Define logfilter Internal namespace level so other files (e.g. texture-cache-manager.cpp) can also use this filter.
83 Debug::Filter* gTextureManagerLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TEXTURE_MANAGER");
86 #define GET_LOAD_STATE_STRING(loadState) \
87 loadState == TextureManagerType::LoadState::NOT_STARTED ? "NOT_STARTED" : \
88 loadState == TextureManagerType::LoadState::LOADING ? "LOADING" : \
89 loadState == TextureManagerType::LoadState::LOAD_FINISHED ? "LOAD_FINISHED" : \
90 loadState == TextureManagerType::LoadState::WAITING_FOR_MASK ? "WAITING_FOR_MASK" : \
91 loadState == TextureManagerType::LoadState::MASK_APPLYING ? "MASK_APPLYING" : \
92 loadState == TextureManagerType::LoadState::MASK_APPLIED ? "MASK_APPLIED" : \
93 loadState == TextureManagerType::LoadState::UPLOADED ? "UPLOADED" : \
94 loadState == TextureManagerType::LoadState::CANCELLED ? "CANCELLED" : \
95 loadState == TextureManagerType::LoadState::LOAD_FAILED ? "LOAD_FAILED" : \
102 const uint32_t DEFAULT_ATLAS_SIZE(1024u); ///< This size can fit 8 by 8 images of average size 128 * 128
103 const Vector4 FULL_ATLAS_RECT(0.0f, 0.0f, 1.0f, 1.0f); ///< UV Rectangle that covers the full Texture
105 void PreMultiply(Devel::PixelBuffer pixelBuffer, TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
107 if(Pixel::HasAlpha(pixelBuffer.GetPixelFormat()))
109 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
111 pixelBuffer.MultiplyColorByAlpha();
116 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
120 } // Anonymous namespace
122 TextureManager::MaskingData::MaskingData()
124 mAlphaMaskId(INVALID_TEXTURE_ID),
125 mContentScaleFactor(1.0f),
127 mPreappliedMasking(true),
128 mMaskImageLoadingFailed(false)
132 TextureManager::TextureManager()
133 : mTextureCacheManager(),
134 mAsyncLocalLoaders(GetNumberOfLocalLoaderThreads(), [&]() { return TextureAsyncLoadingHelper(*this); }),
135 mAsyncRemoteLoaders(GetNumberOfRemoteLoaderThreads(), [&]() { return TextureAsyncLoadingHelper(*this); }),
136 mLifecycleObservers(),
139 mLoadingQueueTextureId(INVALID_TEXTURE_ID),
140 mLoadYuvPlanes(NeedToLoadYuvPlanes())
142 // Initialize the AddOn
143 RenderingAddOn::Get();
146 TextureManager::~TextureManager()
148 for(auto iter = mLifecycleObservers.Begin(), endIter = mLifecycleObservers.End(); iter != endIter; ++iter)
150 (*iter)->TextureManagerDestroyed();
154 TextureSet TextureManager::LoadAnimatedImageTexture(
155 const VisualUrl& url,
156 Dali::AnimatedImageLoading animatedImageLoading,
157 const uint32_t& frameIndex,
158 TextureManager::TextureId& textureId,
159 MaskingDataPointer& maskInfo,
160 const Dali::SamplingMode::Type& samplingMode,
161 const bool& synchronousLoading,
162 TextureUploadObserver* textureObserver,
163 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
165 TextureSet textureSet;
167 if(synchronousLoading)
169 Devel::PixelBuffer pixelBuffer;
170 if(animatedImageLoading)
172 pixelBuffer = animatedImageLoading.LoadFrame(frameIndex);
176 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous loading is failed\n");
181 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
183 Devel::PixelBuffer maskPixelBuffer = LoadImageFromFile(maskInfo->mAlphaMaskUrl.GetUrl(), ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true);
186 if(!maskInfo->mPreappliedMasking)
188 PixelData maskPixelData = Devel::PixelBuffer::Convert(maskPixelBuffer); // takes ownership of buffer
189 maskTexture = Texture::New(Dali::TextureType::TEXTURE_2D, maskPixelData.GetPixelFormat(), maskPixelData.GetWidth(), maskPixelData.GetHeight());
190 maskTexture.Upload(maskPixelData);
194 pixelBuffer.ApplyMask(maskPixelBuffer, maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
199 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous mask image loading is failed\n");
203 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
205 PreMultiply(pixelBuffer, preMultiplyOnLoad);
208 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
211 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight());
212 texture.Upload(pixelData);
213 textureSet = TextureSet::New();
214 textureSet.SetTexture(TEXTURE_INDEX, texture);
217 textureSet.SetTexture(MASK_TEXTURE_INDEX, maskTexture);
224 TextureId alphaMaskId = INVALID_TEXTURE_ID;
225 float contentScaleFactor = 1.0f;
226 bool cropToMask = false;
227 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
229 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, maskInfo->mPreappliedMasking ? StorageType::KEEP_PIXEL_BUFFER : StorageType::KEEP_TEXTURE);
230 alphaMaskId = maskInfo->mAlphaMaskId;
231 if(maskInfo->mPreappliedMasking)
233 contentScaleFactor = maskInfo->mContentScaleFactor;
234 cropToMask = maskInfo->mCropToMask;
238 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);
240 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
241 if(loadState == TextureManager::LoadState::UPLOADED)
243 // LoadComplete has already been called - keep the same texture set
244 textureSet = GetTextureSet(textureId);
251 Devel::PixelBuffer TextureManager::LoadPixelBuffer(
252 const VisualUrl& url,
253 const Dali::ImageDimensions& desiredSize,
254 const Dali::FittingMode::Type& fittingMode,
255 const Dali::SamplingMode::Type& samplingMode,
256 const bool& synchronousLoading,
257 TextureUploadObserver* textureObserver,
258 const bool& orientationCorrection,
259 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
261 Devel::PixelBuffer pixelBuffer;
262 if(synchronousLoading)
266 if(url.IsBufferResource())
268 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
269 if(encodedImageBuffer)
271 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
276 pixelBuffer = LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
278 if(pixelBuffer && preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
280 PreMultiply(pixelBuffer, preMultiplyOnLoad);
286 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);
292 TextureSet TextureManager::LoadTexture(
293 const VisualUrl& url,
294 const Dali::ImageDimensions& desiredSize,
295 const Dali::FittingMode::Type& fittingMode,
296 const Dali::SamplingMode::Type& samplingMode,
297 MaskingDataPointer& maskInfo,
298 const bool& synchronousLoading,
299 TextureManager::TextureId& textureId,
300 Vector4& textureRect,
301 Dali::ImageDimensions& textureRectSize,
302 bool& atlasingStatus,
304 TextureUploadObserver* textureObserver,
305 AtlasUploadObserver* atlasObserver,
306 ImageAtlasManagerPtr imageAtlasManager,
307 const bool& orientationCorrection,
308 const TextureManager::ReloadPolicy& reloadPolicy,
309 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
311 TextureSet textureSet;
313 loadingStatus = false;
314 textureRect = FULL_ATLAS_RECT;
316 if(VisualUrl::TEXTURE == url.GetProtocolType())
318 std::string location = url.GetLocation();
319 if(location.size() > 0u)
321 TextureId id = std::stoi(location);
322 textureSet = mTextureCacheManager.GetExternalTextureSet(id);
325 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
334 if(synchronousLoading && atlasingStatus)
336 const bool synchronousAtlasAvaliable = (desiredSize != ImageDimensions() || url.IsLocalResource()) ? imageAtlasManager->CheckAtlasAvailable(url, desiredSize)
338 if(synchronousAtlasAvaliable)
340 std::vector<Devel::PixelBuffer> pixelBuffers;
341 LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection, false, pixelBuffers);
343 if(!pixelBuffers.empty() && maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
345 std::vector<Devel::PixelBuffer> maskPixelBuffers;
346 LoadImageSynchronously(maskInfo->mAlphaMaskUrl, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true, false, maskPixelBuffers);
347 if(!maskPixelBuffers.empty())
349 pixelBuffers[0].ApplyMask(maskPixelBuffers[0], maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
354 if(!pixelBuffers.empty())
356 PreMultiply(pixelBuffers[0], preMultiplyOnLoad);
357 data = Devel::PixelBuffer::Convert(pixelBuffers[0]); // takes ownership of buffer
361 textureSet = imageAtlasManager->Add(textureRect, data);
364 textureRectSize.SetWidth(data.GetWidth());
365 textureRectSize.SetHeight(data.GetHeight());
370 DALI_LOG_ERROR("TextureManager::LoadTexture: Synchronous Texture loading with atlasing is failed.\n");
375 atlasingStatus = false;
382 loadingStatus = true;
383 // Atlas manager can chage desired size when it is set by 0,0.
384 // We should store into textureRectSize only if atlasing successed.
385 // So copy inputed desiredSize, and replace value into textureRectSize only if atlasing success.
386 Dali::ImageDimensions atlasDesiredSize = desiredSize;
389 if(url.IsBufferResource())
391 const EncodedImageBuffer& encodedImageBuffer = GetEncodedImageBuffer(url.GetUrl());
392 if(encodedImageBuffer)
394 textureSet = imageAtlasManager->Add(textureRect, encodedImageBuffer, desiredSize, fittingMode, true, atlasObserver);
399 textureSet = imageAtlasManager->Add(textureRect, url, atlasDesiredSize, fittingMode, true, atlasObserver);
402 if(!textureSet) // big image, no atlasing or atlasing failed
404 atlasingStatus = false;
406 TextureId alphaMaskId = INVALID_TEXTURE_ID;
407 float contentScaleFactor = 1.0f;
408 bool cropToMask = false;
409 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
411 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, maskInfo->mPreappliedMasking ? StorageType::KEEP_PIXEL_BUFFER : StorageType::KEEP_TEXTURE, synchronousLoading);
412 alphaMaskId = maskInfo->mAlphaMaskId;
413 if(maskInfo && maskInfo->mPreappliedMasking)
415 contentScaleFactor = maskInfo->mContentScaleFactor;
416 cropToMask = maskInfo->mCropToMask;
420 textureId = RequestLoad(
430 orientationCorrection,
435 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
436 if(loadState == TextureManager::LoadState::UPLOADED)
438 // LoadComplete has already been called - keep the same texture set
439 textureSet = GetTextureSet(textureId);
442 // If we are loading the texture, or waiting for the ready signal handler to complete, inform
443 // caller that they need to wait.
444 loadingStatus = (loadState == TextureManager::LoadState::LOADING ||
445 loadState == TextureManager::LoadState::WAITING_FOR_MASK ||
446 loadState == TextureManager::LoadState::MASK_APPLYING ||
447 loadState == TextureManager::LoadState::MASK_APPLIED ||
448 loadState == TextureManager::LoadState::NOT_STARTED ||
449 mLoadingQueueTextureId != INVALID_TEXTURE_ID);
453 textureRectSize = atlasDesiredSize;
458 if(synchronousLoading)
460 loadingStatus = false;
466 TextureManager::TextureId TextureManager::RequestLoad(
467 const VisualUrl& url,
468 const ImageDimensions& desiredSize,
469 const Dali::FittingMode::Type& fittingMode,
470 const Dali::SamplingMode::Type& samplingMode,
471 const UseAtlas& useAtlas,
472 TextureUploadObserver* observer,
473 const bool& orientationCorrection,
474 const TextureManager::ReloadPolicy& reloadPolicy,
475 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
476 const bool& synchronousLoading)
478 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);
481 TextureManager::TextureId TextureManager::RequestLoad(
482 const VisualUrl& url,
483 const TextureManager::TextureId& maskTextureId,
484 const float& contentScale,
485 const Dali::ImageDimensions& desiredSize,
486 const Dali::FittingMode::Type& fittingMode,
487 const Dali::SamplingMode::Type& samplingMode,
488 const TextureManager::UseAtlas& useAtlas,
489 const bool& cropToMask,
490 TextureUploadObserver* observer,
491 const bool& orientationCorrection,
492 const TextureManager::ReloadPolicy& reloadPolicy,
493 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
494 const bool& synchronousLoading)
496 return RequestLoadInternal(url, maskTextureId, contentScale, desiredSize, fittingMode, samplingMode, useAtlas, cropToMask, StorageType::UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
499 TextureManager::TextureId TextureManager::RequestMaskLoad(
500 const VisualUrl& maskUrl,
501 StorageType storageType,
502 const bool& synchronousLoading)
504 // Use the normal load procedure to get the alpha mask.
505 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
506 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);
509 TextureManager::TextureId TextureManager::RequestLoadInternal(
510 const VisualUrl& url,
511 const TextureManager::TextureId& maskTextureId,
512 const float& contentScale,
513 const Dali::ImageDimensions& desiredSize,
514 const Dali::FittingMode::Type& fittingMode,
515 const Dali::SamplingMode::Type& samplingMode,
516 const TextureManager::UseAtlas& useAtlas,
517 const bool& cropToMask,
518 const TextureManager::StorageType& storageType,
519 TextureUploadObserver* observer,
520 const bool& orientationCorrection,
521 const TextureManager::ReloadPolicy& reloadPolicy,
522 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
523 Dali::AnimatedImageLoading animatedImageLoading,
524 const std::uint32_t& frameIndex,
525 const bool& synchronousLoading)
527 TextureHash textureHash = INITIAL_HASH_NUMBER;
528 TextureCacheIndex cacheIndex = INVALID_CACHE_INDEX;
529 bool loadYuvPlanes = (mLoadYuvPlanes && maskTextureId == INVALID_TEXTURE_ID && storageType == StorageType::UPLOAD_TO_TEXTURE);
531 if(storageType != StorageType::RETURN_PIXEL_BUFFER)
533 textureHash = mTextureCacheManager.GenerateHash(url, desiredSize, fittingMode, samplingMode, useAtlas, maskTextureId, cropToMask, frameIndex);
535 // Look up the texture by hash. Note: The extra parameters are used in case of a hash collision.
536 cacheIndex = mTextureCacheManager.FindCachedTexture(textureHash, url, desiredSize, fittingMode, samplingMode, useAtlas, storageType, maskTextureId, cropToMask, preMultiplyOnLoad, (animatedImageLoading) ? true : false, frameIndex);
539 TextureManager::TextureId textureId = INVALID_TEXTURE_ID;
540 // Check if the requested Texture exists in the cache.
541 if(cacheIndex != INVALID_CACHE_INDEX)
543 if(TextureManager::ReloadPolicy::CACHED == reloadPolicy)
545 // Mark this texture being used by another client resource. Forced reload would replace the current texture
546 // without the need for incrementing the reference count.
547 ++(mTextureCacheManager[cacheIndex].referenceCount);
549 textureId = mTextureCacheManager[cacheIndex].textureId;
551 // Update preMultiplyOnLoad value. It should be changed according to preMultiplied value of the cached info.
552 preMultiplyOnLoad = mTextureCacheManager[cacheIndex].preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
554 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);
557 if(textureId == INVALID_TEXTURE_ID) // There was no caching, or caching not required
559 textureId = mTextureCacheManager.GenerateTextureId();
561 bool preMultiply = (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
563 // Cache new texutre, and get cacheIndex.
564 cacheIndex = mTextureCacheManager.AppendCache(TextureInfo(textureId, maskTextureId, url, desiredSize, contentScale, fittingMode, samplingMode, false, cropToMask, useAtlas, textureHash, orientationCorrection, preMultiply, animatedImageLoading, frameIndex, loadYuvPlanes));
566 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);
569 // The below code path is common whether we are using the cache or not.
570 // The textureInfoIndex now refers to either a pre-existing cached TextureInfo,
571 // or a new TextureInfo just created.
572 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
573 textureInfo.maskTextureId = maskTextureId;
574 textureInfo.storageType = storageType;
575 textureInfo.orientationCorrection = orientationCorrection;
577 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureInfo loadState:%s\n", GET_LOAD_STATE_STRING(textureInfo.loadState));
579 // Force reloading of texture by setting loadState unless already loading or cancelled.
580 if(TextureManager::ReloadPolicy::FORCED == reloadPolicy &&
581 TextureManager::LoadState::LOADING != textureInfo.loadState &&
582 TextureManager::LoadState::WAITING_FOR_MASK != textureInfo.loadState &&
583 TextureManager::LoadState::MASK_APPLYING != textureInfo.loadState &&
584 TextureManager::LoadState::MASK_APPLIED != textureInfo.loadState &&
585 TextureManager::LoadState::CANCELLED != textureInfo.loadState)
587 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);
589 textureInfo.loadState = TextureManager::LoadState::NOT_STARTED;
592 if(!synchronousLoading)
594 // Check if we should add the observer.
595 // Only do this if we have not loaded yet and it will not have loaded by the end of this method.
596 switch(textureInfo.loadState)
598 case TextureManager::LoadState::LOAD_FAILED: // Failed notifies observer which then stops observing.
599 case TextureManager::LoadState::NOT_STARTED:
601 LoadOrQueueTexture(textureInfo, observer); // If called inside NotifyObservers, queues until afterwards
604 case TextureManager::LoadState::LOADING:
605 case TextureManager::LoadState::WAITING_FOR_MASK:
606 case TextureManager::LoadState::MASK_APPLYING:
607 case TextureManager::LoadState::MASK_APPLIED:
609 ObserveTexture(textureInfo, observer);
612 case TextureManager::LoadState::UPLOADED:
616 LoadOrQueueTexture(textureInfo, observer);
620 case TextureManager::LoadState::CANCELLED:
622 // A cancelled texture hasn't finished loading yet. Treat as a loading texture
623 // (it's ref count has already been incremented, above)
624 textureInfo.loadState = TextureManager::LoadState::LOADING;
625 ObserveTexture(textureInfo, observer);
628 case TextureManager::LoadState::LOAD_FINISHED:
630 // Loading has already completed.
631 if(observer && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
633 LoadOrQueueTexture(textureInfo, observer);
641 // If the image is already finished to load, use cached texture.
642 // We don't need to consider Observer because this is synchronous loading.
643 if(!(textureInfo.loadState == TextureManager::LoadState::UPLOADED ||
644 textureInfo.loadState == TextureManager::LoadState::LOAD_FINISHED))
646 std::vector<Devel::PixelBuffer> pixelBuffers;
647 LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection, loadYuvPlanes, pixelBuffers);
649 if(pixelBuffers.empty())
651 // If pixelBuffer loading is failed in synchronously, call Remove() method.
652 Remove(textureId, nullptr);
653 return INVALID_TEXTURE_ID;
656 if(storageType == StorageType::KEEP_PIXEL_BUFFER) // For the mask image loading.
658 textureInfo.pixelBuffer = pixelBuffers[0]; // Store the pixel data
659 textureInfo.loadState = LoadState::LOAD_FINISHED;
661 else // For the image loading.
664 if(maskTextureId != INVALID_TEXTURE_ID)
666 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
667 if(maskCacheIndex != INVALID_CACHE_INDEX)
669 if(mTextureCacheManager[maskCacheIndex].storageType == StorageType::KEEP_TEXTURE)
671 if(!mTextureCacheManager[maskCacheIndex].textures.empty())
673 maskTexture = mTextureCacheManager[maskCacheIndex].textures[0];
676 else if(mTextureCacheManager[maskCacheIndex].storageType == StorageType::KEEP_PIXEL_BUFFER)
678 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
681 pixelBuffers[0].ApplyMask(maskPixelBuffer, contentScale, cropToMask);
685 DALI_LOG_ERROR("Mask image cached invalid pixel buffer!\n");
691 DALI_LOG_ERROR("Mask image is not stored in cache.\n");
694 PreMultiply(pixelBuffers[0], preMultiplyOnLoad);
697 UploadTextures(pixelBuffers, textureInfo);
705 void TextureManager::Remove(const TextureManager::TextureId& textureId, TextureUploadObserver* observer)
707 if(textureId != INVALID_TEXTURE_ID)
709 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
710 if(textureCacheIndex != INVALID_CACHE_INDEX)
712 TextureManager::TextureId maskTextureId = INVALID_TEXTURE_ID;
713 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
714 if(textureInfo.maskTextureId != INVALID_TEXTURE_ID)
716 maskTextureId = textureInfo.maskTextureId;
719 // the case that LoadingQueue is working.
720 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
722 // If textureId is not same, this observer need to delete when ProcessRemoveQueue() is called.
723 TextureUploadObserver* queueObserver = nullptr;
724 if(mLoadingQueueTextureId != textureId)
726 queueObserver = observer;
729 // Remove textureId after NotifyObserver finished
730 if(maskTextureId != INVALID_TEXTURE_ID)
732 if(textureInfo.loadState != LoadState::CANCELLED)
734 mRemoveQueue.PushBack(QueueElement(maskTextureId, nullptr));
737 mRemoveQueue.PushBack(QueueElement(textureId, queueObserver));
741 // Remove its observer
742 RemoveTextureObserver(textureInfo, observer);
744 // Keep loadState due to the textureInfo validate problem.
745 auto textureLoadState = textureInfo.loadState;
747 // Remove textureId in CacheManager
748 mTextureCacheManager.RemoveCache(textureInfo);
750 // Remove maskTextureId in CacheManager
751 if(maskTextureId != INVALID_TEXTURE_ID)
753 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
754 if(maskCacheIndex != INVALID_CACHE_INDEX)
756 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
758 // Only Remove maskTexture when texture's loadState is not CANCELLED. because it is already deleted.
759 if(textureLoadState != LoadState::CANCELLED)
761 mTextureCacheManager.RemoveCache(maskTextureInfo);
770 // Remove element from the LoadQueue
771 for(auto&& element : mLoadQueue)
773 if(element.mObserver == observer)
775 // Do not erase the item. We will clear it later in ProcessLoadQueue().
776 element.mObserver = nullptr;
784 void TextureManager::LoadImageSynchronously(
785 const VisualUrl& url,
786 const Dali::ImageDimensions& desiredSize,
787 const Dali::FittingMode::Type& fittingMode,
788 const Dali::SamplingMode::Type& samplingMode,
789 const bool& orientationCorrection,
790 const bool& loadYuvPlanes,
791 std::vector<Devel::PixelBuffer>& pixelBuffers)
793 Devel::PixelBuffer pixelBuffer;
794 if(url.IsBufferResource())
796 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
797 if(encodedImageBuffer)
799 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
806 Dali::LoadImagePlanesFromFile(url.GetUrl(), pixelBuffers, desiredSize, fittingMode, samplingMode, orientationCorrection);
810 pixelBuffer = Dali::LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
816 pixelBuffers.push_back(pixelBuffer);
820 void TextureManager::AddObserver(TextureManager::LifecycleObserver& observer)
822 // make sure an observer doesn't observe the same object twice
823 // otherwise it will get multiple calls to ObjectDestroyed()
824 DALI_ASSERT_DEBUG(mLifecycleObservers.End() == std::find(mLifecycleObservers.Begin(), mLifecycleObservers.End(), &observer));
825 mLifecycleObservers.PushBack(&observer);
828 void TextureManager::RemoveObserver(TextureManager::LifecycleObserver& observer)
830 // Find the observer...
831 auto endIter = mLifecycleObservers.End();
832 for(auto iter = mLifecycleObservers.Begin(); iter != endIter; ++iter)
834 if((*iter) == &observer)
836 mLifecycleObservers.Erase(iter);
840 DALI_ASSERT_DEBUG(endIter != mLifecycleObservers.End());
843 void TextureManager::LoadOrQueueTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
845 switch(textureInfo.loadState)
847 case LoadState::NOT_STARTED:
848 case LoadState::LOAD_FAILED:
850 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
852 QueueLoadTexture(textureInfo, observer);
856 LoadTexture(textureInfo, observer);
860 case LoadState::UPLOADED:
862 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
864 QueueLoadTexture(textureInfo, observer);
868 // The Texture has already loaded. The other observers have already been notified.
869 // We need to send a "late" loaded notification for this observer.
870 EmitLoadComplete(observer, textureInfo, true);
874 case LoadState::LOADING:
875 case LoadState::CANCELLED:
876 case LoadState::LOAD_FINISHED:
877 case LoadState::WAITING_FOR_MASK:
878 case LoadState::MASK_APPLYING:
879 case LoadState::MASK_APPLIED:
886 void TextureManager::QueueLoadTexture(const TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
888 const auto& textureId = textureInfo.textureId;
889 mLoadQueue.PushBack(QueueElement(textureId, observer));
891 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
894 void TextureManager::LoadTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
896 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::LoadTexture(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
898 textureInfo.loadState = LoadState::LOADING;
899 if(!textureInfo.loadSynchronously)
901 auto& loadersContainer = (textureInfo.url.IsLocalResource() || textureInfo.url.IsBufferResource()) ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
902 auto loadingHelperIt = loadersContainer.GetNext();
903 auto premultiplyOnLoad = (textureInfo.preMultiplyOnLoad && textureInfo.maskTextureId == INVALID_TEXTURE_ID) ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
904 DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
905 if(textureInfo.animatedImageLoading)
907 loadingHelperIt->LoadAnimatedImage(textureInfo.textureId, textureInfo.animatedImageLoading, textureInfo.frameIndex, premultiplyOnLoad);
911 loadingHelperIt->Load(textureInfo.textureId, textureInfo.url, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, textureInfo.orientationCorrection, premultiplyOnLoad, textureInfo.loadYuvPlanes);
914 ObserveTexture(textureInfo, observer);
917 void TextureManager::ProcessLoadQueue()
919 for(auto&& element : mLoadQueue)
921 if(!element.mObserver)
926 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(element.mTextureId);
927 if(cacheIndex != INVALID_CACHE_INDEX)
929 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
930 if((textureInfo.loadState == LoadState::UPLOADED) || (textureInfo.loadState == LoadState::LOAD_FINISHED && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER))
932 EmitLoadComplete(element.mObserver, textureInfo, true);
934 else if(textureInfo.loadState == LoadState::LOADING)
936 // Note : LOADING state texture cannot be queue.
937 // This case be occured when same texture id are queue in mLoadQueue.
938 ObserveTexture(textureInfo, element.mObserver);
942 LoadTexture(textureInfo, element.mObserver);
949 void TextureManager::ProcessRemoveQueue()
951 TextureCacheIndex textureCacheIndex = INVALID_CACHE_INDEX;
952 for(auto&& element : mRemoveQueue)
954 textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(element.mTextureId);
955 if(textureCacheIndex != INVALID_CACHE_INDEX)
957 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
958 RemoveTextureObserver(textureInfo, element.mObserver);
959 mTextureCacheManager.RemoveCache(textureInfo);
962 mRemoveQueue.Clear();
965 void TextureManager::ObserveTexture(TextureManager::TextureInfo& textureInfo,
966 TextureUploadObserver* observer)
968 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ObserveTexture(): url:%s observer:%p\n", textureInfo.url.GetUrl().c_str(), observer);
972 textureInfo.observerList.PushBack(observer);
973 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
977 void TextureManager::AsyncLoadComplete(const TextureManager::TextureId& textureId, std::vector<Devel::PixelBuffer>& pixelBuffers)
979 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
980 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::AsyncLoadComplete( textureId:%d CacheIndex:%d )\n", textureId, cacheIndex.GetIndex());
981 if(cacheIndex != INVALID_CACHE_INDEX)
983 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
985 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));
987 if(textureInfo.loadState != LoadState::CANCELLED)
989 // textureInfo can be invalidated after this call (as the mTextureInfoContainer may be modified)
990 PostLoad(textureInfo, pixelBuffers);
994 Remove(textureInfo.textureId, nullptr);
999 void TextureManager::PostLoad(TextureManager::TextureInfo& textureInfo, std::vector<Devel::PixelBuffer>& pixelBuffers)
1001 // Was the load successful?
1002 if(!pixelBuffers.empty())
1004 if(pixelBuffers.size() == 1)
1006 Devel::PixelBuffer pixelBuffer = pixelBuffers[0];
1007 if(pixelBuffer && (pixelBuffer.GetWidth() != 0) && (pixelBuffer.GetHeight() != 0))
1009 // No atlas support for now
1010 textureInfo.useAtlas = UseAtlas::NO_ATLAS;
1011 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
1013 if(textureInfo.storageType == StorageType::UPLOAD_TO_TEXTURE)
1015 // If there is a mask texture ID associated with this texture, then apply the mask
1016 // if it's already loaded. If it hasn't, and the mask is still loading,
1017 // wait for the mask to finish loading.
1018 // note, If the texture is already uploaded synchronously during loading,
1019 // we don't need to apply mask.
1020 if(textureInfo.loadState != LoadState::UPLOADED &&
1021 textureInfo.maskTextureId != INVALID_TEXTURE_ID)
1023 if(textureInfo.loadState == LoadState::MASK_APPLYING)
1025 textureInfo.loadState = LoadState::MASK_APPLIED;
1026 UploadTextures(pixelBuffers, textureInfo);
1027 NotifyObservers(textureInfo, true);
1031 LoadState maskLoadState = mTextureCacheManager.GetTextureStateInternal(textureInfo.maskTextureId);
1032 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
1033 if(maskLoadState == LoadState::LOADING)
1035 textureInfo.loadState = LoadState::WAITING_FOR_MASK;
1037 else if(maskLoadState == LoadState::LOAD_FINISHED || maskLoadState == LoadState::UPLOADED)
1039 // Send New Task to Thread
1040 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureInfo.maskTextureId);
1041 if(maskCacheIndex != INVALID_CACHE_INDEX)
1043 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
1044 if(maskTextureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER)
1046 // Send New Task to Thread
1047 ApplyMask(textureInfo, textureInfo.maskTextureId);
1049 else if(maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1051 // Upload image texture. textureInfo.loadState will be UPLOADED.
1052 UploadTextures(pixelBuffers, textureInfo);
1054 // notify mask texture set.
1055 NotifyObservers(textureInfo, true);
1059 else // maskLoadState == LoadState::LOAD_FAILED
1061 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1062 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1063 UploadTextures(pixelBuffers, textureInfo);
1064 NotifyObservers(textureInfo, true);
1070 UploadTextures(pixelBuffers, textureInfo);
1071 NotifyObservers(textureInfo, true);
1076 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
1077 textureInfo.loadState = LoadState::LOAD_FINISHED;
1079 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1081 NotifyObservers(textureInfo, true);
1083 else // for the StorageType::KEEP_PIXEL_BUFFER and StorageType::KEEP_TEXTURE
1085 // Check if there was another texture waiting for this load to complete
1086 // (e.g. if this was an image mask, and its load is on a different thread)
1087 CheckForWaitingTexture(textureInfo);
1095 // No atlas support for now
1096 textureInfo.useAtlas = UseAtlas::NO_ATLAS;
1097 textureInfo.preMultiplied = false;
1099 UploadTextures(pixelBuffers, textureInfo);
1100 NotifyObservers(textureInfo, true);
1105 textureInfo.loadState = LoadState::LOAD_FAILED;
1106 if(textureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER || textureInfo.storageType == StorageType::KEEP_TEXTURE)
1108 // Check if there was another texture waiting for this load to complete
1109 // (e.g. if this was an image mask, and its load is on a different thread)
1110 CheckForWaitingTexture(textureInfo);
1114 NotifyObservers(textureInfo, false);
1119 void TextureManager::CheckForWaitingTexture(TextureManager::TextureInfo& maskTextureInfo)
1121 if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED &&
1122 maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1124 // Upload mask texture. textureInfo.loadState will be UPLOADED.
1125 std::vector<Devel::PixelBuffer> pixelBuffers;
1126 pixelBuffers.push_back(maskTextureInfo.pixelBuffer);
1127 UploadTextures(pixelBuffers, maskTextureInfo);
1130 // Search the cache, checking if any texture has this texture id as a
1132 const std::size_t size = mTextureCacheManager.size();
1134 // TODO : Refactorize here to not iterate whole cached image.
1135 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1137 if(mTextureCacheManager[cacheIndex].maskTextureId == maskTextureInfo.textureId &&
1138 mTextureCacheManager[cacheIndex].loadState == LoadState::WAITING_FOR_MASK)
1140 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1142 if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED)
1144 if(maskTextureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER)
1146 // Send New Task to Thread
1147 ApplyMask(textureInfo, maskTextureInfo.textureId);
1150 else if(maskTextureInfo.loadState == LoadState::UPLOADED)
1152 if(maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1154 // Upload image texture. textureInfo.loadState will be UPLOADED.
1155 std::vector<Devel::PixelBuffer> pixelBuffers;
1156 pixelBuffers.push_back(textureInfo.pixelBuffer);
1157 UploadTextures(pixelBuffers, textureInfo);
1159 // notify mask texture set.
1160 NotifyObservers(textureInfo, true);
1165 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1166 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1167 std::vector<Devel::PixelBuffer> pixelBuffers;
1168 pixelBuffers.push_back(textureInfo.pixelBuffer);
1169 UploadTextures(pixelBuffers, textureInfo);
1170 NotifyObservers(textureInfo, true);
1176 void TextureManager::ApplyMask(TextureManager::TextureInfo& textureInfo, const TextureManager::TextureId& maskTextureId)
1178 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
1179 if(maskCacheIndex != INVALID_CACHE_INDEX)
1181 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
1182 Devel::PixelBuffer pixelBuffer = textureInfo.pixelBuffer;
1183 textureInfo.pixelBuffer.Reset();
1185 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ApplyMask(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
1187 textureInfo.loadState = LoadState::MASK_APPLYING;
1188 auto& loadersContainer = (textureInfo.url.IsLocalResource() || textureInfo.url.IsBufferResource()) ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
1189 auto loadingHelperIt = loadersContainer.GetNext();
1190 auto premultiplyOnLoad = textureInfo.preMultiplyOnLoad ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
1191 DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
1192 loadingHelperIt->ApplyMask(textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask, premultiplyOnLoad);
1196 void TextureManager::UploadTextures(std::vector<Devel::PixelBuffer>& pixelBuffers, TextureManager::TextureInfo& textureInfo)
1198 if(!pixelBuffers.empty() && textureInfo.loadState != LoadState::UPLOADED && textureInfo.useAtlas != UseAtlas::USE_ATLAS)
1200 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, " TextureManager::UploadTextures() New Texture for textureId:%d\n", textureInfo.textureId);
1202 // Check if this pixelBuffer is premultiplied
1203 textureInfo.preMultiplied = pixelBuffers[0].IsAlphaPreMultiplied();
1205 auto& renderingAddOn = RenderingAddOn::Get();
1206 if(renderingAddOn.IsValid())
1208 renderingAddOn.CreateGeometry(textureInfo.textureId, pixelBuffers[0]);
1211 // Remove previous textures and insert new textures
1212 textureInfo.textures.clear();
1214 for(auto&& pixelBuffer : pixelBuffers)
1216 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight());
1217 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
1218 texture.Upload(pixelData);
1219 textureInfo.textures.push_back(texture);
1223 // Update the load state.
1224 // Note: This is regardless of success as we care about whether a
1225 // load attempt is in progress or not. If unsuccessful, a broken
1226 // image is still loaded.
1227 textureInfo.loadState = LoadState::UPLOADED;
1230 void TextureManager::NotifyObservers(TextureManager::TextureInfo& textureInfo, const bool& success)
1232 TextureId textureId = textureInfo.textureId;
1234 // If there is an observer: Notify the load is complete, whether successful or not,
1235 // and erase it from the list
1236 TextureInfo* info = &textureInfo;
1238 if(info->animatedImageLoading)
1240 // If loading failed, we don't need to get frameCount and frameInterval.
1243 info->frameCount = info->animatedImageLoading.GetImageCount();
1244 info->frameInterval = info->animatedImageLoading.GetFrameInterval(info->frameIndex);
1246 info->animatedImageLoading.Reset();
1249 mLoadingQueueTextureId = textureId;
1251 // Reverse observer list that we can pop_back the observer.
1252 std::reverse(info->observerList.Begin(), info->observerList.End());
1254 while(info->observerList.Count())
1256 TextureUploadObserver* observer = *(info->observerList.End() - 1u);
1258 // During LoadComplete() a Control ResourceReady() signal is emitted.
1259 // During that signal the app may add remove /add Textures (e.g. via
1261 // It is possible for observers to be removed from the observer list,
1262 // and it is also possible for the mTextureInfoContainer to be modified,
1263 // invalidating the reference to the textureInfo struct.
1264 // Texture load requests for the same URL are deferred until the end of this
1266 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));
1268 // It is possible for the observer to be deleted.
1269 // Disconnect and remove the observer first.
1270 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1272 info->observerList.Erase(info->observerList.End() - 1u);
1274 EmitLoadComplete(observer, *info, success);
1276 // Get the textureInfo from the container again as it may have been invalidated.
1277 TextureCacheIndex textureInfoIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1278 if(textureInfoIndex == INVALID_CACHE_INDEX)
1280 break; // texture has been removed - can stop.
1282 info = &mTextureCacheManager[textureInfoIndex];
1285 mLoadingQueueTextureId = INVALID_TEXTURE_ID;
1287 ProcessRemoveQueue();
1289 if(info->storageType == StorageType::RETURN_PIXEL_BUFFER && info->observerList.Count() == 0)
1291 Remove(info->textureId, nullptr);
1295 void TextureManager::ObserverDestroyed(TextureUploadObserver* observer)
1297 const std::size_t size = mTextureCacheManager.size();
1298 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1300 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1301 for(TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin();
1302 j != textureInfo.observerList.End();)
1306 j = textureInfo.observerList.Erase(j);
1315 // Remove element from the LoadQueue
1316 for(auto&& element : mLoadQueue)
1318 if(element.mObserver == observer)
1320 element.mObserver = nullptr;
1325 Dali::Geometry TextureManager::GetRenderGeometry(const TextureManager::TextureId& textureId, std::uint32_t& frontElements, std::uint32_t& backElements)
1327 return RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().GetGeometry(textureId, frontElements, backElements) : Geometry();
1330 void TextureManager::EmitLoadComplete(TextureUploadObserver* observer, TextureManager::TextureInfo& textureInfo, const bool& success)
1332 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1334 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::PIXEL_BUFFER, textureInfo.pixelBuffer, textureInfo.url.GetUrl(), textureInfo.preMultiplied));
1338 TextureSet textureSet = GetTextureSet(textureInfo);
1339 if(textureInfo.isAnimatedImageFormat)
1341 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::ANIMATED_IMAGE_TEXTURE, textureInfo.textureId, textureSet, textureInfo.frameCount, textureInfo.frameInterval));
1345 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::TEXTURE, textureInfo.textureId, textureSet, (textureInfo.useAtlas == UseAtlas::USE_ATLAS) ? true : false, textureInfo.atlasRect, textureInfo.preMultiplied));
1350 TextureSet TextureManager::GetTextureSet(const TextureManager::TextureId& textureId)
1352 TextureSet textureSet;
1353 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
1354 if(loadState == TextureManager::LoadState::UPLOADED)
1356 // LoadComplete has already been called - keep the same texture set
1357 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1358 if(textureCacheIndex != INVALID_CACHE_INDEX)
1360 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
1361 textureSet = GetTextureSet(textureInfo);
1366 DALI_LOG_ERROR("GetTextureSet is failed. texture is not uploaded \n");
1371 TextureSet TextureManager::GetTextureSet(const TextureManager::TextureInfo& textureInfo)
1373 TextureSet textureSet;
1375 // LoadComplete has already been called - keep the same texture set
1376 textureSet = TextureSet::New();
1377 if(!textureInfo.textures.empty())
1379 if(textureInfo.textures.size() > 1) // For YUV case
1381 uint32_t index = 0u;
1382 for(auto&& texture : textureInfo.textures)
1384 textureSet.SetTexture(index++, texture);
1389 textureSet.SetTexture(TEXTURE_INDEX, textureInfo.textures[0]);
1390 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureInfo.maskTextureId);
1391 if(maskCacheIndex != INVALID_CACHE_INDEX)
1393 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
1394 if(maskTextureInfo.storageType == StorageType::UPLOAD_TO_TEXTURE || maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1396 if(!maskTextureInfo.textures.empty())
1398 textureSet.SetTexture(MASK_TEXTURE_INDEX, maskTextureInfo.textures[0]);
1407 void TextureManager::RemoveTextureObserver(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
1409 // Remove its observer
1412 const auto iterEnd = textureInfo.observerList.End();
1413 const auto iter = std::find(textureInfo.observerList.Begin(), iterEnd, observer);
1416 // Disconnect and remove the observer.
1417 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1418 textureInfo.observerList.Erase(iter);
1423 } // namespace Internal
1425 } // namespace Toolkit