2 * Copyright (c) 2023 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 <dali-toolkit/internal/texture-manager/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/adaptor-framework/adaptor.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/public-api/rendering/geometry.h>
30 #include <dali-toolkit/internal/texture-manager/texture-async-loading-helper.h>
31 #include <dali-toolkit/internal/texture-manager/texture-cache-manager.h>
32 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
33 #include <dali-toolkit/internal/visuals/rendering-addon.h>
37 constexpr auto INITIAL_HASH_NUMBER = size_t{0u};
39 constexpr auto TEXTURE_INDEX = 0u; ///< The Index for texture
40 constexpr auto MASK_TEXTURE_INDEX = 1u; ///< The Index for mask texture
42 constexpr auto NUMBER_OF_LOCAL_LOADER_THREADS_ENV = "DALI_TEXTURE_LOCAL_THREADS";
43 constexpr auto NUMBER_OF_REMOTE_LOADER_THREADS_ENV = "DALI_TEXTURE_REMOTE_THREADS";
44 constexpr auto LOAD_IMAGE_YUV_PLANES_ENV = "DALI_LOAD_IMAGE_YUV_PLANES";
46 bool NeedToLoadYuvPlanes()
48 auto loadYuvPlanesString = Dali::EnvironmentVariable::GetEnvironmentVariable(LOAD_IMAGE_YUV_PLANES_ENV);
49 bool loadYuvPlanes = loadYuvPlanesString ? std::atoi(loadYuvPlanesString) : false;
61 // Define logfilter Internal namespace level so other files (e.g. texture-cache-manager.cpp) can also use this filter.
62 Debug::Filter* gTextureManagerLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TEXTURE_MANAGER");
65 #define GET_LOAD_STATE_STRING(loadState) \
66 loadState == TextureManagerType::LoadState::NOT_STARTED ? "NOT_STARTED" : \
67 loadState == TextureManagerType::LoadState::LOADING ? "LOADING" : \
68 loadState == TextureManagerType::LoadState::LOAD_FINISHED ? "LOAD_FINISHED" : \
69 loadState == TextureManagerType::LoadState::WAITING_FOR_MASK ? "WAITING_FOR_MASK" : \
70 loadState == TextureManagerType::LoadState::MASK_APPLYING ? "MASK_APPLYING" : \
71 loadState == TextureManagerType::LoadState::MASK_APPLIED ? "MASK_APPLIED" : \
72 loadState == TextureManagerType::LoadState::UPLOADED ? "UPLOADED" : \
73 loadState == TextureManagerType::LoadState::CANCELLED ? "CANCELLED" : \
74 loadState == TextureManagerType::LoadState::MASK_CANCELLED ? "MASK_CANCELLED" : \
75 loadState == TextureManagerType::LoadState::LOAD_FAILED ? "LOAD_FAILED" : \
82 const uint32_t DEFAULT_ATLAS_SIZE(1024u); ///< This size can fit 8 by 8 images of average size 128 * 128
83 const Vector4 FULL_ATLAS_RECT(0.0f, 0.0f, 1.0f, 1.0f); ///< UV Rectangle that covers the full Texture
85 void PreMultiply(Devel::PixelBuffer pixelBuffer, TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
87 if(Pixel::HasAlpha(pixelBuffer.GetPixelFormat()))
89 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
91 pixelBuffer.MultiplyColorByAlpha();
96 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
100 } // Anonymous namespace
102 TextureManager::MaskingData::MaskingData()
104 mAlphaMaskId(INVALID_TEXTURE_ID),
105 mContentScaleFactor(1.0f),
107 mPreappliedMasking(true),
108 mMaskImageLoadingFailed(false)
112 TextureManager::TextureManager()
113 : mTextureCacheManager(),
114 mAsyncLoader(std::unique_ptr<TextureAsyncLoadingHelper>(new TextureAsyncLoadingHelper(*this))),
115 mLifecycleObservers(),
117 mLoadingQueueTextureId(INVALID_TEXTURE_ID),
119 mLoadYuvPlanes(NeedToLoadYuvPlanes()),
120 mRemoveProcessorRegistered(false)
122 // Initialize the AddOn
123 RenderingAddOn::Get();
126 TextureManager::~TextureManager()
128 if(mRemoveProcessorRegistered && Adaptor::IsAvailable())
130 Adaptor::Get().UnregisterProcessor(*this, true);
131 mRemoveProcessorRegistered = false;
134 for(auto iter = mLifecycleObservers.Begin(), endIter = mLifecycleObservers.End(); iter != endIter; ++iter)
136 (*iter)->TextureManagerDestroyed();
140 TextureSet TextureManager::LoadAnimatedImageTexture(
141 const VisualUrl& url,
142 Dali::AnimatedImageLoading animatedImageLoading,
143 const uint32_t& frameIndex,
144 TextureManager::TextureId& textureId,
145 MaskingDataPointer& maskInfo,
146 const Dali::ImageDimensions& desiredSize,
147 const Dali::FittingMode::Type& fittingMode,
148 const Dali::SamplingMode::Type& samplingMode,
149 const bool& synchronousLoading,
150 TextureUploadObserver* textureObserver,
151 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
153 TextureSet textureSet;
155 if(synchronousLoading)
157 Devel::PixelBuffer pixelBuffer;
158 if(animatedImageLoading)
160 pixelBuffer = animatedImageLoading.LoadFrame(frameIndex, desiredSize, fittingMode, samplingMode);
164 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous loading is failed\n");
169 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
171 Devel::PixelBuffer maskPixelBuffer = LoadImageFromFile(maskInfo->mAlphaMaskUrl.GetUrl(), desiredSize, fittingMode, samplingMode, true);
174 if(!maskInfo->mPreappliedMasking)
176 PixelData maskPixelData = Devel::PixelBuffer::Convert(maskPixelBuffer); // takes ownership of buffer
177 maskTexture = Texture::New(Dali::TextureType::TEXTURE_2D, maskPixelData.GetPixelFormat(), maskPixelData.GetWidth(), maskPixelData.GetHeight());
178 maskTexture.Upload(maskPixelData);
182 pixelBuffer.ApplyMask(maskPixelBuffer, maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
187 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous mask image loading is failed\n");
191 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
193 PreMultiply(pixelBuffer, preMultiplyOnLoad);
196 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
199 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight());
200 texture.Upload(pixelData);
201 textureSet = TextureSet::New();
202 textureSet.SetTexture(TEXTURE_INDEX, texture);
205 textureSet.SetTexture(MASK_TEXTURE_INDEX, maskTexture);
212 TextureId alphaMaskId = INVALID_TEXTURE_ID;
213 float contentScaleFactor = 1.0f;
214 bool cropToMask = false;
215 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
217 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, maskInfo->mPreappliedMasking ? StorageType::KEEP_PIXEL_BUFFER : StorageType::KEEP_TEXTURE);
218 alphaMaskId = maskInfo->mAlphaMaskId;
219 if(maskInfo->mPreappliedMasking)
221 contentScaleFactor = maskInfo->mContentScaleFactor;
222 cropToMask = maskInfo->mCropToMask;
226 textureId = RequestLoadInternal(url, alphaMaskId, contentScaleFactor, desiredSize, fittingMode, samplingMode, UseAtlas::NO_ATLAS, cropToMask, StorageType::UPLOAD_TO_TEXTURE, textureObserver, true, TextureManager::ReloadPolicy::CACHED, preMultiplyOnLoad, animatedImageLoading, frameIndex, false);
228 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
229 if(loadState == TextureManager::LoadState::UPLOADED)
231 // LoadComplete has already been called - keep the same texture set
232 textureSet = GetTextureSet(textureId);
239 Devel::PixelBuffer TextureManager::LoadPixelBuffer(
240 const VisualUrl& url,
241 const Dali::ImageDimensions& desiredSize,
242 const Dali::FittingMode::Type& fittingMode,
243 const Dali::SamplingMode::Type& samplingMode,
244 const bool& synchronousLoading,
245 TextureUploadObserver* textureObserver,
246 const bool& orientationCorrection,
247 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
249 Devel::PixelBuffer pixelBuffer;
250 if(synchronousLoading)
254 if(url.IsBufferResource())
256 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
257 if(encodedImageBuffer)
259 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
264 pixelBuffer = LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
266 if(pixelBuffer && preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
268 PreMultiply(pixelBuffer, preMultiplyOnLoad);
274 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);
280 TextureSet TextureManager::LoadTexture(
281 const VisualUrl& url,
282 const Dali::ImageDimensions& desiredSize,
283 const Dali::FittingMode::Type& fittingMode,
284 const Dali::SamplingMode::Type& samplingMode,
285 MaskingDataPointer& maskInfo,
286 const bool& synchronousLoading,
287 TextureManager::TextureId& textureId,
288 Vector4& textureRect,
289 Dali::ImageDimensions& textureRectSize,
290 bool& atlasingStatus,
292 TextureUploadObserver* textureObserver,
293 AtlasUploadObserver* atlasObserver,
294 ImageAtlasManagerPtr imageAtlasManager,
295 const bool& orientationCorrection,
296 const TextureManager::ReloadPolicy& reloadPolicy,
297 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
299 TextureSet textureSet;
301 loadingStatus = false;
302 textureRect = FULL_ATLAS_RECT;
304 if(VisualUrl::TEXTURE == url.GetProtocolType())
306 std::string location = url.GetLocation();
307 if(location.size() > 0u)
309 TextureId id = std::stoi(location);
310 auto externalTextureInfo = mTextureCacheManager.GetExternalTextureInfo(id);
311 if(externalTextureInfo.textureSet)
315 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
317 // Change preMultiplyOnLoad value so make caller determine to preMultiplyAlpha or not.
318 // TODO : Should we seperate input and output value?
319 preMultiplyOnLoad = externalTextureInfo.preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
321 return externalTextureInfo.textureSet;
328 if(synchronousLoading && atlasingStatus)
330 const bool synchronousAtlasAvaliable = (desiredSize != ImageDimensions() || url.IsLocalResource()) ? imageAtlasManager->CheckAtlasAvailable(url, desiredSize)
332 if(synchronousAtlasAvaliable)
334 std::vector<Devel::PixelBuffer> pixelBuffers;
335 LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection, false, pixelBuffers);
337 if(!pixelBuffers.empty() && maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
339 std::vector<Devel::PixelBuffer> maskPixelBuffers;
340 LoadImageSynchronously(maskInfo->mAlphaMaskUrl, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true, false, maskPixelBuffers);
341 if(!maskPixelBuffers.empty())
343 pixelBuffers[0].ApplyMask(maskPixelBuffers[0], maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
348 if(!pixelBuffers.empty())
350 PreMultiply(pixelBuffers[0], preMultiplyOnLoad);
351 data = Devel::PixelBuffer::Convert(pixelBuffers[0]); // takes ownership of buffer
355 textureSet = imageAtlasManager->Add(textureRect, data);
358 textureRectSize.SetWidth(data.GetWidth());
359 textureRectSize.SetHeight(data.GetHeight());
364 DALI_LOG_ERROR("TextureManager::LoadTexture: Synchronous Texture loading with atlasing is failed.\n");
369 atlasingStatus = false;
376 loadingStatus = true;
377 // Atlas manager can chage desired size when it is set by 0,0.
378 // We should store into textureRectSize only if atlasing successed.
379 // So copy inputed desiredSize, and replace value into textureRectSize only if atlasing success.
380 Dali::ImageDimensions atlasDesiredSize = desiredSize;
383 if(url.IsBufferResource())
385 const EncodedImageBuffer& encodedImageBuffer = GetEncodedImageBuffer(url.GetUrl());
386 if(encodedImageBuffer)
388 textureSet = imageAtlasManager->Add(textureRect, encodedImageBuffer, desiredSize, fittingMode, true, atlasObserver);
393 textureSet = imageAtlasManager->Add(textureRect, url, atlasDesiredSize, fittingMode, true, atlasObserver);
396 if(!textureSet) // big image, no atlasing or atlasing failed
398 atlasingStatus = false;
400 TextureId alphaMaskId = INVALID_TEXTURE_ID;
401 float contentScaleFactor = 1.0f;
402 bool cropToMask = false;
403 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
405 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, maskInfo->mPreappliedMasking ? StorageType::KEEP_PIXEL_BUFFER : StorageType::KEEP_TEXTURE, synchronousLoading);
406 alphaMaskId = maskInfo->mAlphaMaskId;
407 if(maskInfo && maskInfo->mPreappliedMasking)
409 contentScaleFactor = maskInfo->mContentScaleFactor;
410 cropToMask = maskInfo->mCropToMask;
414 textureId = RequestLoad(
424 orientationCorrection,
429 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
430 if(loadState == TextureManager::LoadState::UPLOADED)
432 // LoadComplete has already been called - keep the same texture set
433 textureSet = GetTextureSet(textureId);
436 // If we are loading the texture, or waiting for the ready signal handler to complete, inform
437 // caller that they need to wait.
438 loadingStatus = (loadState == TextureManager::LoadState::LOADING ||
439 loadState == TextureManager::LoadState::WAITING_FOR_MASK ||
440 loadState == TextureManager::LoadState::MASK_APPLYING ||
441 loadState == TextureManager::LoadState::MASK_APPLIED ||
442 loadState == TextureManager::LoadState::NOT_STARTED ||
443 mLoadingQueueTextureId != INVALID_TEXTURE_ID);
447 textureRectSize = atlasDesiredSize;
452 if(synchronousLoading)
454 loadingStatus = false;
460 TextureManager::TextureId TextureManager::RequestLoad(
461 const VisualUrl& url,
462 const ImageDimensions& desiredSize,
463 const Dali::FittingMode::Type& fittingMode,
464 const Dali::SamplingMode::Type& samplingMode,
465 const UseAtlas& useAtlas,
466 TextureUploadObserver* observer,
467 const bool& orientationCorrection,
468 const TextureManager::ReloadPolicy& reloadPolicy,
469 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
470 const bool& synchronousLoading)
472 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);
475 TextureManager::TextureId TextureManager::RequestLoad(
476 const VisualUrl& url,
477 const TextureManager::TextureId& maskTextureId,
478 const float& contentScale,
479 const Dali::ImageDimensions& desiredSize,
480 const Dali::FittingMode::Type& fittingMode,
481 const Dali::SamplingMode::Type& samplingMode,
482 const TextureManager::UseAtlas& useAtlas,
483 const bool& cropToMask,
484 TextureUploadObserver* observer,
485 const bool& orientationCorrection,
486 const TextureManager::ReloadPolicy& reloadPolicy,
487 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
488 const bool& synchronousLoading)
490 return RequestLoadInternal(url, maskTextureId, contentScale, desiredSize, fittingMode, samplingMode, useAtlas, cropToMask, StorageType::UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
493 TextureManager::TextureId TextureManager::RequestMaskLoad(
494 const VisualUrl& maskUrl,
495 StorageType storageType,
496 const bool& synchronousLoading)
498 // Use the normal load procedure to get the alpha mask.
499 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
500 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);
503 TextureManager::TextureId TextureManager::RequestLoadInternal(
504 const VisualUrl& url,
505 const TextureManager::TextureId& maskTextureId,
506 const float& contentScale,
507 const Dali::ImageDimensions& desiredSize,
508 const Dali::FittingMode::Type& fittingMode,
509 const Dali::SamplingMode::Type& samplingMode,
510 const TextureManager::UseAtlas& useAtlas,
511 const bool& cropToMask,
512 const TextureManager::StorageType& storageType,
513 TextureUploadObserver* observer,
514 const bool& orientationCorrection,
515 const TextureManager::ReloadPolicy& reloadPolicy,
516 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
517 Dali::AnimatedImageLoading animatedImageLoading,
518 const std::uint32_t& frameIndex,
519 const bool& synchronousLoading)
521 TextureHash textureHash = INITIAL_HASH_NUMBER;
522 TextureCacheIndex cacheIndex = INVALID_CACHE_INDEX;
523 bool loadYuvPlanes = (mLoadYuvPlanes && maskTextureId == INVALID_TEXTURE_ID && storageType == StorageType::UPLOAD_TO_TEXTURE);
525 if(storageType != StorageType::RETURN_PIXEL_BUFFER)
527 textureHash = mTextureCacheManager.GenerateHash(url, desiredSize, fittingMode, samplingMode, useAtlas, maskTextureId, cropToMask, frameIndex);
529 // Look up the texture by hash. Note: The extra parameters are used in case of a hash collision.
530 cacheIndex = mTextureCacheManager.FindCachedTexture(textureHash, url, desiredSize, fittingMode, samplingMode, useAtlas, storageType, maskTextureId, cropToMask, preMultiplyOnLoad, (animatedImageLoading) ? true : false, frameIndex);
533 TextureManager::TextureId textureId = INVALID_TEXTURE_ID;
534 // Check if the requested Texture exists in the cache.
535 if(cacheIndex != INVALID_CACHE_INDEX)
537 if(TextureManager::ReloadPolicy::CACHED == reloadPolicy)
539 // Mark this texture being used by another client resource. Forced reload would replace the current texture
540 // without the need for incrementing the reference count.
541 ++(mTextureCacheManager[cacheIndex].referenceCount);
543 textureId = mTextureCacheManager[cacheIndex].textureId;
545 // Update preMultiplyOnLoad value. It should be changed according to preMultiplied value of the cached info.
546 preMultiplyOnLoad = mTextureCacheManager[cacheIndex].preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
547 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::RequestLoad( url=%s observer=%p ) Using cached texture id@%d, textureId=%d, maskTextureId=%d, frameindex=%d, premultiplied=%d\n", url.GetUrl().c_str(), observer, cacheIndex.GetIndex(), textureId, maskTextureId, frameIndex, mTextureCacheManager[cacheIndex].preMultiplied ? 1 : 0);
550 if(textureId == INVALID_TEXTURE_ID) // There was no caching, or caching not required
552 textureId = mTextureCacheManager.GenerateTextureId();
554 bool preMultiply = (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
556 // Cache new texutre, and get cacheIndex.
557 cacheIndex = mTextureCacheManager.AppendCache(TextureInfo(textureId, maskTextureId, url, desiredSize, contentScale, fittingMode, samplingMode, false, cropToMask, useAtlas, textureHash, orientationCorrection, preMultiply, animatedImageLoading, frameIndex, loadYuvPlanes));
558 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::RequestLoad( url=%s observer=%p ) New texture, cacheIndex:%d, textureId=%d, maskTextureId=%d, frameindex=%d premultiply=%d\n", url.GetUrl().c_str(), observer, cacheIndex.GetIndex(), textureId, maskTextureId, frameIndex, preMultiply);
561 // The below code path is common whether we are using the cache or not.
562 // The textureInfoIndex now refers to either a pre-existing cached TextureInfo,
563 // or a new TextureInfo just created.
564 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
565 textureInfo.maskTextureId = maskTextureId;
566 textureInfo.storageType = storageType;
567 textureInfo.orientationCorrection = orientationCorrection;
569 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureInfo loadState:%s\n", GET_LOAD_STATE_STRING(textureInfo.loadState));
571 // Force reloading of texture by setting loadState unless already loading or cancelled.
572 if(TextureManager::ReloadPolicy::FORCED == reloadPolicy &&
573 TextureManager::LoadState::LOADING != textureInfo.loadState &&
574 TextureManager::LoadState::WAITING_FOR_MASK != textureInfo.loadState &&
575 TextureManager::LoadState::MASK_APPLYING != textureInfo.loadState &&
576 TextureManager::LoadState::MASK_APPLIED != textureInfo.loadState &&
577 TextureManager::LoadState::CANCELLED != textureInfo.loadState &&
578 TextureManager::LoadState::MASK_CANCELLED != textureInfo.loadState)
580 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, "TextureManager::RequestLoad( url=%s observer=%p ) ForcedReload cacheIndex:%d, textureId=%d, maskTextureId=%d\n", url.GetUrl().c_str(), observer, cacheIndex.GetIndex(), textureId, maskTextureId);
581 textureInfo.loadState = TextureManager::LoadState::NOT_STARTED;
584 if(!synchronousLoading)
586 // Check if we should add the observer.
587 // Only do this if we have not loaded yet and it will not have loaded by the end of this method.
588 switch(textureInfo.loadState)
590 case TextureManager::LoadState::LOAD_FAILED: // Failed notifies observer which then stops observing.
591 case TextureManager::LoadState::NOT_STARTED:
593 LoadOrQueueTexture(textureInfo, observer); // If called inside NotifyObservers, queues until afterwards
596 case TextureManager::LoadState::LOADING:
597 case TextureManager::LoadState::WAITING_FOR_MASK:
598 case TextureManager::LoadState::MASK_APPLYING:
599 case TextureManager::LoadState::MASK_APPLIED:
601 ObserveTexture(textureInfo, observer);
604 case TextureManager::LoadState::UPLOADED:
608 LoadOrQueueTexture(textureInfo, observer);
612 case TextureManager::LoadState::CANCELLED:
614 // A cancelled texture hasn't finished loading yet. Treat as a loading texture
615 // (it's ref count has already been incremented, above)
616 textureInfo.loadState = TextureManager::LoadState::LOADING;
617 ObserveTexture(textureInfo, observer);
620 case TextureManager::LoadState::MASK_CANCELLED:
622 // A cancelled texture hasn't finished mask applying yet. Treat as a mask applying texture
623 // (it's ref count has already been incremented, above)
624 textureInfo.loadState = TextureManager::LoadState::MASK_APPLYING;
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 RequestRemove() method.
652 RequestRemove(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::RequestRemove(const TextureManager::TextureId& textureId, TextureUploadObserver* observer)
707 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::RequestRemove( textureId=%d observer=%p )\n", textureId, observer);
710 if(textureId != INVALID_TEXTURE_ID)
714 // Remove observer from cached texture info
715 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
716 if(textureCacheIndex != INVALID_CACHE_INDEX)
718 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
719 RemoveTextureObserver(textureInfo, observer);
722 mRemoveQueue.PushBack(textureId);
724 if(!mRemoveProcessorRegistered && Adaptor::IsAvailable())
726 mRemoveProcessorRegistered = true;
727 Adaptor::Get().RegisterProcessor(*this, true);
732 void TextureManager::Remove(const TextureManager::TextureId& textureId)
734 if(textureId != INVALID_TEXTURE_ID)
736 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
737 if(textureCacheIndex != INVALID_CACHE_INDEX)
739 TextureManager::TextureId maskTextureId = INVALID_TEXTURE_ID;
740 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
741 // We only need to consider maskTextureId when texture's loadState is not cancelled. Because it is already deleted.
742 if(textureInfo.loadState != LoadState::CANCELLED && textureInfo.loadState != LoadState::MASK_CANCELLED)
744 if(textureInfo.maskTextureId != INVALID_TEXTURE_ID)
746 maskTextureId = textureInfo.maskTextureId;
750 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::Remove( textureId=%d ) cacheIndex:%d removal maskTextureId=%d, loadingQueueTextureId=%d, loadState=%s\n", textureId, textureCacheIndex.GetIndex(), maskTextureId, mLoadingQueueTextureId, GET_LOAD_STATE_STRING(textureInfo.loadState));
752 // Remove textureId in CacheManager. Now, textureInfo is invalidate.
753 mTextureCacheManager.RemoveCache(textureInfo);
755 // Remove maskTextureId in CacheManager
756 if(maskTextureId != INVALID_TEXTURE_ID)
758 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
759 if(maskCacheIndex != INVALID_CACHE_INDEX)
761 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
762 mTextureCacheManager.RemoveCache(maskTextureInfo);
769 void TextureManager::ProcessRemoveQueue()
771 // Note that RemoveQueue is not be changed during Remove().
772 for(auto&& textureId : mRemoveQueue)
774 if(textureId != INVALID_TEXTURE_ID)
779 mRemoveQueue.Clear();
782 void TextureManager::Process(bool postProcessor)
784 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::Process()\n");
786 ProcessRemoveQueue();
788 if(Adaptor::IsAvailable())
790 Adaptor::Get().UnregisterProcessor(*this, true);
791 mRemoveProcessorRegistered = false;
795 void TextureManager::LoadImageSynchronously(
796 const VisualUrl& url,
797 const Dali::ImageDimensions& desiredSize,
798 const Dali::FittingMode::Type& fittingMode,
799 const Dali::SamplingMode::Type& samplingMode,
800 const bool& orientationCorrection,
801 const bool& loadYuvPlanes,
802 std::vector<Devel::PixelBuffer>& pixelBuffers)
804 Devel::PixelBuffer pixelBuffer;
805 if(url.IsBufferResource())
807 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
808 if(encodedImageBuffer)
810 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
817 Dali::LoadImagePlanesFromFile(url.GetUrl(), pixelBuffers, desiredSize, fittingMode, samplingMode, orientationCorrection);
821 pixelBuffer = Dali::LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
827 pixelBuffers.push_back(pixelBuffer);
831 void TextureManager::AddObserver(TextureManager::LifecycleObserver& observer)
833 // make sure an observer doesn't observe the same object twice
834 // otherwise it will get multiple calls to ObjectDestroyed()
835 DALI_ASSERT_DEBUG(mLifecycleObservers.End() == std::find(mLifecycleObservers.Begin(), mLifecycleObservers.End(), &observer));
836 mLifecycleObservers.PushBack(&observer);
839 void TextureManager::RemoveObserver(TextureManager::LifecycleObserver& observer)
841 // Find the observer...
842 auto endIter = mLifecycleObservers.End();
843 for(auto iter = mLifecycleObservers.Begin(); iter != endIter; ++iter)
845 if((*iter) == &observer)
847 mLifecycleObservers.Erase(iter);
851 DALI_ASSERT_DEBUG(endIter != mLifecycleObservers.End());
854 void TextureManager::LoadOrQueueTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
856 switch(textureInfo.loadState)
858 case LoadState::NOT_STARTED:
859 case LoadState::LOAD_FAILED:
861 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
863 QueueLoadTexture(textureInfo, observer);
867 LoadTexture(textureInfo, observer);
871 case LoadState::UPLOADED:
873 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
875 QueueLoadTexture(textureInfo, observer);
879 // The Texture has already loaded. The other observers have already been notified.
880 // We need to send a "late" loaded notification for this observer.
881 EmitLoadComplete(observer, textureInfo, true);
885 case LoadState::LOADING:
886 case LoadState::CANCELLED:
887 case LoadState::MASK_CANCELLED:
888 case LoadState::LOAD_FINISHED:
889 case LoadState::WAITING_FOR_MASK:
890 case LoadState::MASK_APPLYING:
891 case LoadState::MASK_APPLIED:
898 void TextureManager::QueueLoadTexture(const TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
900 const auto& textureId = textureInfo.textureId;
901 mLoadQueue.PushBack(QueueElement(textureId, observer));
905 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
909 void TextureManager::LoadTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
911 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::LoadTexture(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
912 textureInfo.loadState = LoadState::LOADING;
913 if(!textureInfo.loadSynchronously)
915 auto premultiplyOnLoad = (textureInfo.preMultiplyOnLoad && textureInfo.maskTextureId == INVALID_TEXTURE_ID) ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
916 if(textureInfo.animatedImageLoading)
918 mAsyncLoader->LoadAnimatedImage(textureInfo.textureId, textureInfo.animatedImageLoading, textureInfo.frameIndex, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, premultiplyOnLoad);
922 mAsyncLoader->Load(textureInfo.textureId, textureInfo.url, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, textureInfo.orientationCorrection, premultiplyOnLoad, textureInfo.loadYuvPlanes);
925 ObserveTexture(textureInfo, observer);
928 void TextureManager::ProcessLoadQueue()
930 for(auto&& element : mLoadQueue)
932 if(element.mTextureId == INVALID_TEXTURE_ID)
937 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(element.mTextureId);
938 if(cacheIndex != INVALID_CACHE_INDEX)
940 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
941 if((textureInfo.loadState == LoadState::UPLOADED) || (textureInfo.loadState == LoadState::LOAD_FINISHED && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER))
943 if(element.mObserver)
945 EmitLoadComplete(element.mObserver, textureInfo, true);
948 else if(textureInfo.loadState == LoadState::LOADING)
950 // Note : LOADING state texture cannot be queue.
951 // This case be occured when same texture id are queue in mLoadQueue.
952 ObserveTexture(textureInfo, element.mObserver);
956 LoadTexture(textureInfo, element.mObserver);
963 void TextureManager::ObserveTexture(TextureManager::TextureInfo& textureInfo,
964 TextureUploadObserver* observer)
966 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ObserveTexture(): url:%s observer:%p\n", textureInfo.url.GetUrl().c_str(), observer);
970 textureInfo.observerList.PushBack(observer);
971 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
975 void TextureManager::AsyncLoadComplete(const TextureManager::TextureId& textureId, std::vector<Devel::PixelBuffer>& pixelBuffers)
977 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
978 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::AsyncLoadComplete( textureId:%d CacheIndex:%d )\n", textureId, cacheIndex.GetIndex());
979 if(cacheIndex != INVALID_CACHE_INDEX)
981 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
983 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));
984 if(textureInfo.loadState != LoadState::CANCELLED && textureInfo.loadState != LoadState::MASK_CANCELLED)
986 // textureInfo can be invalidated after this call (as the mTextureInfoContainer may be modified)
987 PostLoad(textureInfo, pixelBuffers);
991 RequestRemove(textureInfo.textureId, nullptr);
996 void TextureManager::PostLoad(TextureManager::TextureInfo& textureInfo, std::vector<Devel::PixelBuffer>& pixelBuffers)
998 // Was the load successful?
999 if(!pixelBuffers.empty())
1001 if(pixelBuffers.size() == 1)
1003 Devel::PixelBuffer pixelBuffer = pixelBuffers[0];
1004 if(pixelBuffer && (pixelBuffer.GetWidth() != 0) && (pixelBuffer.GetHeight() != 0))
1006 // No atlas support for now
1007 textureInfo.useAtlas = UseAtlas::NO_ATLAS;
1008 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
1010 if(textureInfo.storageType == StorageType::UPLOAD_TO_TEXTURE)
1012 // If there is a mask texture ID associated with this texture, then apply the mask
1013 // if it's already loaded. If it hasn't, and the mask is still loading,
1014 // wait for the mask to finish loading.
1015 // note, If the texture is already uploaded synchronously during loading,
1016 // we don't need to apply mask.
1017 if(textureInfo.loadState != LoadState::UPLOADED &&
1018 textureInfo.maskTextureId != INVALID_TEXTURE_ID)
1020 if(textureInfo.loadState == LoadState::MASK_APPLYING)
1022 textureInfo.loadState = LoadState::MASK_APPLIED;
1023 UploadTextures(pixelBuffers, textureInfo);
1024 NotifyObservers(textureInfo, true);
1028 LoadState maskLoadState = mTextureCacheManager.GetTextureStateInternal(textureInfo.maskTextureId);
1029 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
1030 if(maskLoadState == LoadState::LOADING)
1032 textureInfo.loadState = LoadState::WAITING_FOR_MASK;
1034 else if(maskLoadState == LoadState::LOAD_FINISHED || maskLoadState == LoadState::UPLOADED)
1036 // Send New Task to Thread
1037 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureInfo.maskTextureId);
1038 if(maskCacheIndex != INVALID_CACHE_INDEX)
1040 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
1041 if(maskTextureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER)
1043 // Send New Task to Thread
1044 ApplyMask(textureInfo, textureInfo.maskTextureId);
1046 else if(maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1048 // Upload image texture. textureInfo.loadState will be UPLOADED.
1049 UploadTextures(pixelBuffers, textureInfo);
1051 // notify mask texture set.
1052 NotifyObservers(textureInfo, true);
1056 else // maskLoadState == LoadState::LOAD_FAILED
1058 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1059 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1060 UploadTextures(pixelBuffers, textureInfo);
1061 NotifyObservers(textureInfo, true);
1067 UploadTextures(pixelBuffers, textureInfo);
1068 NotifyObservers(textureInfo, true);
1073 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
1074 textureInfo.loadState = LoadState::LOAD_FINISHED;
1076 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1078 NotifyObservers(textureInfo, true);
1080 else // for the StorageType::KEEP_PIXEL_BUFFER and StorageType::KEEP_TEXTURE
1082 // Check if there was another texture waiting for this load to complete
1083 // (e.g. if this was an image mask, and its load is on a different thread)
1084 CheckForWaitingTexture(textureInfo);
1092 // No atlas support for now
1093 textureInfo.useAtlas = UseAtlas::NO_ATLAS;
1094 textureInfo.preMultiplied = false;
1096 UploadTextures(pixelBuffers, textureInfo);
1097 NotifyObservers(textureInfo, true);
1102 textureInfo.loadState = LoadState::LOAD_FAILED;
1103 if(textureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER || textureInfo.storageType == 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);
1111 NotifyObservers(textureInfo, false);
1116 void TextureManager::CheckForWaitingTexture(TextureManager::TextureInfo& maskTextureInfo)
1118 if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED &&
1119 maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1121 // Upload mask texture. textureInfo.loadState will be UPLOADED.
1122 std::vector<Devel::PixelBuffer> pixelBuffers;
1123 pixelBuffers.push_back(maskTextureInfo.pixelBuffer);
1124 UploadTextures(pixelBuffers, maskTextureInfo);
1127 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): maskTextureId=%d, maskTextureUrl=%s\n", maskTextureInfo.textureId, maskTextureInfo.url.GetUrl().c_str());
1129 // Search the cache, checking if any texture has this texture id as a maskTextureId
1130 const std::size_t size = mTextureCacheManager.size();
1132 // Keep notify observer required textureIds.
1133 // Note : NotifyObservers can change mTextureCacheManager cache struct. We should check id's validation before notify.
1134 std::vector<TextureId> notifyRequiredTextureIds;
1136 // TODO : Refactorize here to not iterate whole cached image.
1137 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1139 if(mTextureCacheManager[cacheIndex].maskTextureId == maskTextureInfo.textureId &&
1140 mTextureCacheManager[cacheIndex].loadState == LoadState::WAITING_FOR_MASK)
1142 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1144 if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED)
1146 if(maskTextureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER)
1148 // Send New Task to Thread
1149 ApplyMask(textureInfo, maskTextureInfo.textureId);
1152 else if(maskTextureInfo.loadState == LoadState::UPLOADED)
1154 if(maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1156 // Upload image texture. textureInfo.loadState will be UPLOADED.
1157 std::vector<Devel::PixelBuffer> pixelBuffers;
1158 pixelBuffers.push_back(textureInfo.pixelBuffer);
1159 UploadTextures(pixelBuffers, textureInfo);
1161 // Increase reference counts for notify required textureId.
1162 // Now we can assume that we don't remove & re-assign this textureId
1163 // during NotifyObserver signal emit.
1164 maskTextureInfo.referenceCount++;
1165 textureInfo.referenceCount++;
1167 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): Ready to notify textureId=%d\n", textureInfo.textureId);
1169 notifyRequiredTextureIds.push_back(textureInfo.textureId);
1172 else // maskTextureInfo.loadState == LoadState::LOAD_FAILED
1174 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1175 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1176 std::vector<Devel::PixelBuffer> pixelBuffers;
1177 pixelBuffers.push_back(textureInfo.pixelBuffer);
1178 UploadTextures(pixelBuffers, textureInfo);
1180 // Increase reference counts for notify required textureId.
1181 // Now we can assume that we don't remove & re-assign this textureId
1182 // during NotifyObserver signal emit.
1183 maskTextureInfo.referenceCount++;
1184 textureInfo.referenceCount++;
1186 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): Ready to notify textureId=%d\n", textureInfo.textureId);
1188 notifyRequiredTextureIds.push_back(textureInfo.textureId);
1193 // Notify textures are masked
1194 for(const auto textureId : notifyRequiredTextureIds)
1196 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1197 if(textureCacheIndex != INVALID_CACHE_INDEX)
1199 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
1200 NotifyObservers(textureInfo, true);
1204 // Decrease reference count
1205 for(const auto textureId : notifyRequiredTextureIds)
1207 RequestRemove(textureId, nullptr);
1211 void TextureManager::ApplyMask(TextureManager::TextureInfo& textureInfo, const TextureManager::TextureId& maskTextureId)
1213 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
1214 if(maskCacheIndex != INVALID_CACHE_INDEX)
1216 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
1217 Devel::PixelBuffer pixelBuffer = textureInfo.pixelBuffer;
1218 textureInfo.pixelBuffer.Reset();
1220 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ApplyMask(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
1222 textureInfo.loadState = LoadState::MASK_APPLYING;
1223 auto premultiplyOnLoad = textureInfo.preMultiplyOnLoad ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
1224 mAsyncLoader->ApplyMask(textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask, premultiplyOnLoad);
1228 void TextureManager::UploadTextures(std::vector<Devel::PixelBuffer>& pixelBuffers, TextureManager::TextureInfo& textureInfo)
1230 if(!pixelBuffers.empty() && textureInfo.loadState != LoadState::UPLOADED && textureInfo.useAtlas != UseAtlas::USE_ATLAS)
1232 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, " TextureManager::UploadTextures() New Texture for textureId:%d\n", textureInfo.textureId);
1234 // Check if this pixelBuffer is premultiplied
1235 textureInfo.preMultiplied = pixelBuffers[0].IsAlphaPreMultiplied();
1237 auto& renderingAddOn = RenderingAddOn::Get();
1238 if(renderingAddOn.IsValid())
1240 renderingAddOn.CreateGeometry(textureInfo.textureId, pixelBuffers[0]);
1243 // Remove previous textures and insert new textures
1244 textureInfo.textures.clear();
1246 for(auto&& pixelBuffer : pixelBuffers)
1248 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight());
1249 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
1250 texture.Upload(pixelData);
1251 textureInfo.textures.push_back(texture);
1255 // Update the load state.
1256 // Note: This is regardless of success as we care about whether a
1257 // load attempt is in progress or not. If unsuccessful, a broken
1258 // image is still loaded.
1259 textureInfo.loadState = LoadState::UPLOADED;
1262 void TextureManager::NotifyObservers(TextureManager::TextureInfo& textureInfo, const bool& success)
1264 TextureId textureId = textureInfo.textureId;
1266 // If there is an observer: Notify the load is complete, whether successful or not,
1267 // and erase it from the list
1268 TextureInfo* info = &textureInfo;
1270 if(info->animatedImageLoading)
1272 // If loading failed, we don't need to get frameCount and frameInterval.
1275 info->frameCount = info->animatedImageLoading.GetImageCount();
1276 info->frameInterval = info->animatedImageLoading.GetFrameInterval(info->frameIndex);
1278 info->animatedImageLoading.Reset();
1281 mLoadingQueueTextureId = textureId;
1283 // Reverse observer list that we can pop_back the observer.
1284 std::reverse(info->observerList.Begin(), info->observerList.End());
1286 while(info->observerList.Count())
1288 TextureUploadObserver* observer = *(info->observerList.End() - 1u);
1290 // During LoadComplete() a Control ResourceReady() signal is emitted.
1291 // During that signal the app may add remove /add Textures (e.g. via
1293 // It is possible for observers to be removed from the observer list,
1294 // and it is also possible for the mTextureInfoContainer to be modified,
1295 // invalidating the reference to the textureInfo struct.
1296 // Texture load requests for the same URL are deferred until the end of this
1298 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));
1299 // It is possible for the observer to be deleted.
1300 // Disconnect and remove the observer first.
1301 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1303 info->observerList.Erase(info->observerList.End() - 1u);
1305 EmitLoadComplete(observer, *info, success);
1307 // Get the textureInfo from the container again as it may have been invalidated.
1308 TextureCacheIndex textureInfoIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1309 if(textureInfoIndex == INVALID_CACHE_INDEX)
1311 break; // texture has been removed - can stop.
1313 info = &mTextureCacheManager[textureInfoIndex];
1316 mLoadingQueueTextureId = INVALID_TEXTURE_ID;
1319 if(info->storageType == StorageType::RETURN_PIXEL_BUFFER && info->observerList.Count() == 0)
1321 RequestRemove(info->textureId, nullptr);
1325 void TextureManager::ObserverDestroyed(TextureUploadObserver* observer)
1327 const std::size_t size = mTextureCacheManager.size();
1328 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1330 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1331 for(TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin();
1332 j != textureInfo.observerList.End();)
1336 j = textureInfo.observerList.Erase(j);
1345 // Remove element from the LoadQueue
1346 for(auto&& element : mLoadQueue)
1348 if(element.mObserver == observer)
1350 element.mTextureId = INVALID_TEXTURE_ID;
1351 element.mObserver = nullptr;
1356 Dali::Geometry TextureManager::GetRenderGeometry(const TextureManager::TextureId& textureId, std::uint32_t& frontElements, std::uint32_t& backElements)
1358 return RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().GetGeometry(textureId, frontElements, backElements) : Geometry();
1361 void TextureManager::EmitLoadComplete(TextureUploadObserver* observer, TextureManager::TextureInfo& textureInfo, const bool& success)
1363 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1365 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::PIXEL_BUFFER, textureInfo.pixelBuffer, textureInfo.url.GetUrl(), textureInfo.preMultiplied));
1369 TextureSet textureSet = GetTextureSet(textureInfo);
1370 if(textureInfo.isAnimatedImageFormat)
1372 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::ANIMATED_IMAGE_TEXTURE, textureInfo.textureId, textureSet, textureInfo.frameCount, textureInfo.frameInterval));
1376 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::TEXTURE, textureInfo.textureId, textureSet, (textureInfo.useAtlas == UseAtlas::USE_ATLAS) ? true : false, textureInfo.atlasRect, textureInfo.preMultiplied));
1381 TextureSet TextureManager::GetTextureSet(const TextureManager::TextureId& textureId)
1383 TextureSet textureSet;
1384 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
1385 if(loadState == TextureManager::LoadState::UPLOADED)
1387 // LoadComplete has already been called - keep the same texture set
1388 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1389 if(textureCacheIndex != INVALID_CACHE_INDEX)
1391 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
1392 textureSet = GetTextureSet(textureInfo);
1397 DALI_LOG_ERROR("GetTextureSet is failed. texture is not uploaded \n");
1402 TextureSet TextureManager::GetTextureSet(const TextureManager::TextureInfo& textureInfo)
1404 TextureSet textureSet;
1406 // LoadComplete has already been called - keep the same texture set
1407 textureSet = TextureSet::New();
1408 if(!textureInfo.textures.empty())
1410 if(textureInfo.textures.size() > 1) // For YUV case
1412 uint32_t index = 0u;
1413 for(auto&& texture : textureInfo.textures)
1415 textureSet.SetTexture(index++, texture);
1420 textureSet.SetTexture(TEXTURE_INDEX, textureInfo.textures[0]);
1421 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureInfo.maskTextureId);
1422 if(maskCacheIndex != INVALID_CACHE_INDEX)
1424 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
1425 if(maskTextureInfo.storageType == StorageType::UPLOAD_TO_TEXTURE || maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1427 if(!maskTextureInfo.textures.empty())
1429 textureSet.SetTexture(MASK_TEXTURE_INDEX, maskTextureInfo.textures[0]);
1438 void TextureManager::RemoveTextureObserver(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
1440 // Remove its observer
1443 const auto iterEnd = textureInfo.observerList.End();
1444 const auto iter = std::find(textureInfo.observerList.Begin(), iterEnd, observer);
1447 // Disconnect and remove the observer.
1448 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1449 textureInfo.observerList.Erase(iter);
1454 } // namespace Internal
1456 } // namespace Toolkit