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/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};
38 constexpr auto TEXTURE_INDEX = 0u; ///< The Index for texture
39 constexpr auto MASK_TEXTURE_INDEX = 1u; ///< The Index for mask texture
41 constexpr auto NUMBER_OF_LOCAL_LOADER_THREADS_ENV = "DALI_TEXTURE_LOCAL_THREADS";
42 constexpr auto NUMBER_OF_REMOTE_LOADER_THREADS_ENV = "DALI_TEXTURE_REMOTE_THREADS";
43 constexpr auto LOAD_IMAGE_YUV_PLANES_ENV = "DALI_LOAD_IMAGE_YUV_PLANES";
45 bool NeedToLoadYuvPlanes()
47 auto loadYuvPlanesString = Dali::EnvironmentVariable::GetEnvironmentVariable(LOAD_IMAGE_YUV_PLANES_ENV);
48 bool loadYuvPlanes = loadYuvPlanesString ? std::atoi(loadYuvPlanesString) : false;
60 // Define logfilter Internal namespace level so other files (e.g. texture-cache-manager.cpp) can also use this filter.
61 Debug::Filter* gTextureManagerLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TEXTURE_MANAGER");
64 #define GET_LOAD_STATE_STRING(loadState) \
65 loadState == TextureManagerType::LoadState::NOT_STARTED ? "NOT_STARTED" : \
66 loadState == TextureManagerType::LoadState::LOADING ? "LOADING" : \
67 loadState == TextureManagerType::LoadState::LOAD_FINISHED ? "LOAD_FINISHED" : \
68 loadState == TextureManagerType::LoadState::WAITING_FOR_MASK ? "WAITING_FOR_MASK" : \
69 loadState == TextureManagerType::LoadState::MASK_APPLYING ? "MASK_APPLYING" : \
70 loadState == TextureManagerType::LoadState::MASK_APPLIED ? "MASK_APPLIED" : \
71 loadState == TextureManagerType::LoadState::UPLOADED ? "UPLOADED" : \
72 loadState == TextureManagerType::LoadState::CANCELLED ? "CANCELLED" : \
73 loadState == TextureManagerType::LoadState::MASK_CANCELLED ? "MASK_CANCELLED" : \
74 loadState == TextureManagerType::LoadState::LOAD_FAILED ? "LOAD_FAILED" : \
81 const uint32_t DEFAULT_ATLAS_SIZE(1024u); ///< This size can fit 8 by 8 images of average size 128 * 128
82 const Vector4 FULL_ATLAS_RECT(0.0f, 0.0f, 1.0f, 1.0f); ///< UV Rectangle that covers the full Texture
84 void PreMultiply(Devel::PixelBuffer pixelBuffer, TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
86 if(Pixel::HasAlpha(pixelBuffer.GetPixelFormat()))
88 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
90 pixelBuffer.MultiplyColorByAlpha();
95 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
99 } // Anonymous namespace
101 TextureManager::MaskingData::MaskingData()
103 mAlphaMaskId(INVALID_TEXTURE_ID),
104 mContentScaleFactor(1.0f),
106 mPreappliedMasking(true),
107 mMaskImageLoadingFailed(false)
111 TextureManager::TextureManager()
112 : mTextureCacheManager(),
113 mAsyncLoader(std::unique_ptr<TextureAsyncLoadingHelper>(new TextureAsyncLoadingHelper(*this))),
114 mLifecycleObservers(),
117 mLoadingQueueTextureId(INVALID_TEXTURE_ID),
118 mLoadYuvPlanes(NeedToLoadYuvPlanes())
120 // Initialize the AddOn
121 RenderingAddOn::Get();
124 TextureManager::~TextureManager()
126 for(auto iter = mLifecycleObservers.Begin(), endIter = mLifecycleObservers.End(); iter != endIter; ++iter)
128 (*iter)->TextureManagerDestroyed();
132 TextureSet TextureManager::LoadAnimatedImageTexture(
133 const VisualUrl& url,
134 Dali::AnimatedImageLoading animatedImageLoading,
135 const uint32_t& frameIndex,
136 TextureManager::TextureId& textureId,
137 MaskingDataPointer& maskInfo,
138 const Dali::ImageDimensions& desiredSize,
139 const Dali::FittingMode::Type& fittingMode,
140 const Dali::SamplingMode::Type& samplingMode,
141 const bool& synchronousLoading,
142 TextureUploadObserver* textureObserver,
143 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
145 TextureSet textureSet;
147 if(synchronousLoading)
149 Devel::PixelBuffer pixelBuffer;
150 if(animatedImageLoading)
152 pixelBuffer = animatedImageLoading.LoadFrame(frameIndex, desiredSize, fittingMode, samplingMode);
156 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous loading is failed\n");
161 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
163 Devel::PixelBuffer maskPixelBuffer = LoadImageFromFile(maskInfo->mAlphaMaskUrl.GetUrl(), desiredSize, fittingMode, samplingMode, true);
166 if(!maskInfo->mPreappliedMasking)
168 PixelData maskPixelData = Devel::PixelBuffer::Convert(maskPixelBuffer); // takes ownership of buffer
169 maskTexture = Texture::New(Dali::TextureType::TEXTURE_2D, maskPixelData.GetPixelFormat(), maskPixelData.GetWidth(), maskPixelData.GetHeight());
170 maskTexture.Upload(maskPixelData);
174 pixelBuffer.ApplyMask(maskPixelBuffer, maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
179 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous mask image loading is failed\n");
183 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
185 PreMultiply(pixelBuffer, preMultiplyOnLoad);
188 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
191 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight());
192 texture.Upload(pixelData);
193 textureSet = TextureSet::New();
194 textureSet.SetTexture(TEXTURE_INDEX, texture);
197 textureSet.SetTexture(MASK_TEXTURE_INDEX, maskTexture);
204 TextureId alphaMaskId = INVALID_TEXTURE_ID;
205 float contentScaleFactor = 1.0f;
206 bool cropToMask = false;
207 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
209 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, maskInfo->mPreappliedMasking ? StorageType::KEEP_PIXEL_BUFFER : StorageType::KEEP_TEXTURE);
210 alphaMaskId = maskInfo->mAlphaMaskId;
211 if(maskInfo->mPreappliedMasking)
213 contentScaleFactor = maskInfo->mContentScaleFactor;
214 cropToMask = maskInfo->mCropToMask;
218 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);
220 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
221 if(loadState == TextureManager::LoadState::UPLOADED)
223 // LoadComplete has already been called - keep the same texture set
224 textureSet = GetTextureSet(textureId);
231 Devel::PixelBuffer TextureManager::LoadPixelBuffer(
232 const VisualUrl& url,
233 const Dali::ImageDimensions& desiredSize,
234 const Dali::FittingMode::Type& fittingMode,
235 const Dali::SamplingMode::Type& samplingMode,
236 const bool& synchronousLoading,
237 TextureUploadObserver* textureObserver,
238 const bool& orientationCorrection,
239 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
241 Devel::PixelBuffer pixelBuffer;
242 if(synchronousLoading)
246 if(url.IsBufferResource())
248 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
249 if(encodedImageBuffer)
251 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
256 pixelBuffer = LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
258 if(pixelBuffer && preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
260 PreMultiply(pixelBuffer, preMultiplyOnLoad);
266 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);
272 TextureSet TextureManager::LoadTexture(
273 const VisualUrl& url,
274 const Dali::ImageDimensions& desiredSize,
275 const Dali::FittingMode::Type& fittingMode,
276 const Dali::SamplingMode::Type& samplingMode,
277 MaskingDataPointer& maskInfo,
278 const bool& synchronousLoading,
279 TextureManager::TextureId& textureId,
280 Vector4& textureRect,
281 Dali::ImageDimensions& textureRectSize,
282 bool& atlasingStatus,
284 TextureUploadObserver* textureObserver,
285 AtlasUploadObserver* atlasObserver,
286 ImageAtlasManagerPtr imageAtlasManager,
287 const bool& orientationCorrection,
288 const TextureManager::ReloadPolicy& reloadPolicy,
289 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
291 TextureSet textureSet;
293 loadingStatus = false;
294 textureRect = FULL_ATLAS_RECT;
296 if(VisualUrl::TEXTURE == url.GetProtocolType())
298 std::string location = url.GetLocation();
299 if(location.size() > 0u)
301 TextureId id = std::stoi(location);
302 auto externalTextureInfo = mTextureCacheManager.GetExternalTextureInfo(id);
303 if(externalTextureInfo.textureSet)
307 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
309 // Change preMultiplyOnLoad value so make caller determine to preMultiplyAlpha or not.
310 // TODO : Should we seperate input and output value?
311 preMultiplyOnLoad = externalTextureInfo.preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
313 return externalTextureInfo.textureSet;
320 if(synchronousLoading && atlasingStatus)
322 const bool synchronousAtlasAvaliable = (desiredSize != ImageDimensions() || url.IsLocalResource()) ? imageAtlasManager->CheckAtlasAvailable(url, desiredSize)
324 if(synchronousAtlasAvaliable)
326 std::vector<Devel::PixelBuffer> pixelBuffers;
327 LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection, false, pixelBuffers);
329 if(!pixelBuffers.empty() && maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
331 std::vector<Devel::PixelBuffer> maskPixelBuffers;
332 LoadImageSynchronously(maskInfo->mAlphaMaskUrl, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true, false, maskPixelBuffers);
333 if(!maskPixelBuffers.empty())
335 pixelBuffers[0].ApplyMask(maskPixelBuffers[0], maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
340 if(!pixelBuffers.empty())
342 PreMultiply(pixelBuffers[0], preMultiplyOnLoad);
343 data = Devel::PixelBuffer::Convert(pixelBuffers[0]); // takes ownership of buffer
347 textureSet = imageAtlasManager->Add(textureRect, data);
350 textureRectSize.SetWidth(data.GetWidth());
351 textureRectSize.SetHeight(data.GetHeight());
356 DALI_LOG_ERROR("TextureManager::LoadTexture: Synchronous Texture loading with atlasing is failed.\n");
361 atlasingStatus = false;
368 loadingStatus = true;
369 // Atlas manager can chage desired size when it is set by 0,0.
370 // We should store into textureRectSize only if atlasing successed.
371 // So copy inputed desiredSize, and replace value into textureRectSize only if atlasing success.
372 Dali::ImageDimensions atlasDesiredSize = desiredSize;
375 if(url.IsBufferResource())
377 const EncodedImageBuffer& encodedImageBuffer = GetEncodedImageBuffer(url.GetUrl());
378 if(encodedImageBuffer)
380 textureSet = imageAtlasManager->Add(textureRect, encodedImageBuffer, desiredSize, fittingMode, true, atlasObserver);
385 textureSet = imageAtlasManager->Add(textureRect, url, atlasDesiredSize, fittingMode, true, atlasObserver);
388 if(!textureSet) // big image, no atlasing or atlasing failed
390 atlasingStatus = false;
392 TextureId alphaMaskId = INVALID_TEXTURE_ID;
393 float contentScaleFactor = 1.0f;
394 bool cropToMask = false;
395 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
397 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, maskInfo->mPreappliedMasking ? StorageType::KEEP_PIXEL_BUFFER : StorageType::KEEP_TEXTURE, synchronousLoading);
398 alphaMaskId = maskInfo->mAlphaMaskId;
399 if(maskInfo && maskInfo->mPreappliedMasking)
401 contentScaleFactor = maskInfo->mContentScaleFactor;
402 cropToMask = maskInfo->mCropToMask;
406 textureId = RequestLoad(
416 orientationCorrection,
421 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
422 if(loadState == TextureManager::LoadState::UPLOADED)
424 // LoadComplete has already been called - keep the same texture set
425 textureSet = GetTextureSet(textureId);
428 // If we are loading the texture, or waiting for the ready signal handler to complete, inform
429 // caller that they need to wait.
430 loadingStatus = (loadState == TextureManager::LoadState::LOADING ||
431 loadState == TextureManager::LoadState::WAITING_FOR_MASK ||
432 loadState == TextureManager::LoadState::MASK_APPLYING ||
433 loadState == TextureManager::LoadState::MASK_APPLIED ||
434 loadState == TextureManager::LoadState::NOT_STARTED ||
435 mLoadingQueueTextureId != INVALID_TEXTURE_ID);
439 textureRectSize = atlasDesiredSize;
444 if(synchronousLoading)
446 loadingStatus = false;
452 TextureManager::TextureId TextureManager::RequestLoad(
453 const VisualUrl& url,
454 const ImageDimensions& desiredSize,
455 const Dali::FittingMode::Type& fittingMode,
456 const Dali::SamplingMode::Type& samplingMode,
457 const UseAtlas& useAtlas,
458 TextureUploadObserver* observer,
459 const bool& orientationCorrection,
460 const TextureManager::ReloadPolicy& reloadPolicy,
461 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
462 const bool& synchronousLoading)
464 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);
467 TextureManager::TextureId TextureManager::RequestLoad(
468 const VisualUrl& url,
469 const TextureManager::TextureId& maskTextureId,
470 const float& contentScale,
471 const Dali::ImageDimensions& desiredSize,
472 const Dali::FittingMode::Type& fittingMode,
473 const Dali::SamplingMode::Type& samplingMode,
474 const TextureManager::UseAtlas& useAtlas,
475 const bool& cropToMask,
476 TextureUploadObserver* observer,
477 const bool& orientationCorrection,
478 const TextureManager::ReloadPolicy& reloadPolicy,
479 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
480 const bool& synchronousLoading)
482 return RequestLoadInternal(url, maskTextureId, contentScale, desiredSize, fittingMode, samplingMode, useAtlas, cropToMask, StorageType::UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
485 TextureManager::TextureId TextureManager::RequestMaskLoad(
486 const VisualUrl& maskUrl,
487 StorageType storageType,
488 const bool& synchronousLoading)
490 // Use the normal load procedure to get the alpha mask.
491 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
492 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);
495 TextureManager::TextureId TextureManager::RequestLoadInternal(
496 const VisualUrl& url,
497 const TextureManager::TextureId& maskTextureId,
498 const float& contentScale,
499 const Dali::ImageDimensions& desiredSize,
500 const Dali::FittingMode::Type& fittingMode,
501 const Dali::SamplingMode::Type& samplingMode,
502 const TextureManager::UseAtlas& useAtlas,
503 const bool& cropToMask,
504 const TextureManager::StorageType& storageType,
505 TextureUploadObserver* observer,
506 const bool& orientationCorrection,
507 const TextureManager::ReloadPolicy& reloadPolicy,
508 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
509 Dali::AnimatedImageLoading animatedImageLoading,
510 const std::uint32_t& frameIndex,
511 const bool& synchronousLoading)
513 TextureHash textureHash = INITIAL_HASH_NUMBER;
514 TextureCacheIndex cacheIndex = INVALID_CACHE_INDEX;
515 bool loadYuvPlanes = (mLoadYuvPlanes && maskTextureId == INVALID_TEXTURE_ID && storageType == StorageType::UPLOAD_TO_TEXTURE);
517 if(storageType != StorageType::RETURN_PIXEL_BUFFER)
519 textureHash = mTextureCacheManager.GenerateHash(url, desiredSize, fittingMode, samplingMode, useAtlas, maskTextureId, cropToMask, frameIndex);
521 // Look up the texture by hash. Note: The extra parameters are used in case of a hash collision.
522 cacheIndex = mTextureCacheManager.FindCachedTexture(textureHash, url, desiredSize, fittingMode, samplingMode, useAtlas, storageType, maskTextureId, cropToMask, preMultiplyOnLoad, (animatedImageLoading) ? true : false, frameIndex);
525 TextureManager::TextureId textureId = INVALID_TEXTURE_ID;
526 // Check if the requested Texture exists in the cache.
527 if(cacheIndex != INVALID_CACHE_INDEX)
529 if(TextureManager::ReloadPolicy::CACHED == reloadPolicy)
531 // Mark this texture being used by another client resource. Forced reload would replace the current texture
532 // without the need for incrementing the reference count.
533 ++(mTextureCacheManager[cacheIndex].referenceCount);
535 textureId = mTextureCacheManager[cacheIndex].textureId;
537 // Update preMultiplyOnLoad value. It should be changed according to preMultiplied value of the cached info.
538 preMultiplyOnLoad = mTextureCacheManager[cacheIndex].preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
539 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);
542 if(textureId == INVALID_TEXTURE_ID) // There was no caching, or caching not required
544 textureId = mTextureCacheManager.GenerateTextureId();
546 bool preMultiply = (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
548 // Cache new texutre, and get cacheIndex.
549 cacheIndex = mTextureCacheManager.AppendCache(TextureInfo(textureId, maskTextureId, url, desiredSize, contentScale, fittingMode, samplingMode, false, cropToMask, useAtlas, textureHash, orientationCorrection, preMultiply, animatedImageLoading, frameIndex, loadYuvPlanes));
550 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);
553 // The below code path is common whether we are using the cache or not.
554 // The textureInfoIndex now refers to either a pre-existing cached TextureInfo,
555 // or a new TextureInfo just created.
556 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
557 textureInfo.maskTextureId = maskTextureId;
558 textureInfo.storageType = storageType;
559 textureInfo.orientationCorrection = orientationCorrection;
561 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureInfo loadState:%s\n", GET_LOAD_STATE_STRING(textureInfo.loadState));
563 // Force reloading of texture by setting loadState unless already loading or cancelled.
564 if(TextureManager::ReloadPolicy::FORCED == reloadPolicy &&
565 TextureManager::LoadState::LOADING != textureInfo.loadState &&
566 TextureManager::LoadState::WAITING_FOR_MASK != textureInfo.loadState &&
567 TextureManager::LoadState::MASK_APPLYING != textureInfo.loadState &&
568 TextureManager::LoadState::MASK_APPLIED != textureInfo.loadState &&
569 TextureManager::LoadState::CANCELLED != textureInfo.loadState &&
570 TextureManager::LoadState::MASK_CANCELLED != textureInfo.loadState)
572 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);
573 textureInfo.loadState = TextureManager::LoadState::NOT_STARTED;
576 if(!synchronousLoading)
578 // Check if we should add the observer.
579 // Only do this if we have not loaded yet and it will not have loaded by the end of this method.
580 switch(textureInfo.loadState)
582 case TextureManager::LoadState::LOAD_FAILED: // Failed notifies observer which then stops observing.
583 case TextureManager::LoadState::NOT_STARTED:
585 LoadOrQueueTexture(textureInfo, observer); // If called inside NotifyObservers, queues until afterwards
588 case TextureManager::LoadState::LOADING:
589 case TextureManager::LoadState::WAITING_FOR_MASK:
590 case TextureManager::LoadState::MASK_APPLYING:
591 case TextureManager::LoadState::MASK_APPLIED:
593 ObserveTexture(textureInfo, observer);
596 case TextureManager::LoadState::UPLOADED:
600 LoadOrQueueTexture(textureInfo, observer);
604 case TextureManager::LoadState::CANCELLED:
606 // A cancelled texture hasn't finished loading yet. Treat as a loading texture
607 // (it's ref count has already been incremented, above)
608 textureInfo.loadState = TextureManager::LoadState::LOADING;
609 ObserveTexture(textureInfo, observer);
612 case TextureManager::LoadState::MASK_CANCELLED:
614 // A cancelled texture hasn't finished mask applying yet. Treat as a mask applying texture
615 // (it's ref count has already been incremented, above)
616 textureInfo.loadState = TextureManager::LoadState::MASK_APPLYING;
617 ObserveTexture(textureInfo, observer);
620 case TextureManager::LoadState::LOAD_FINISHED:
622 // Loading has already completed.
623 if(observer && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
625 LoadOrQueueTexture(textureInfo, observer);
633 // If the image is already finished to load, use cached texture.
634 // We don't need to consider Observer because this is synchronous loading.
635 if(!(textureInfo.loadState == TextureManager::LoadState::UPLOADED ||
636 textureInfo.loadState == TextureManager::LoadState::LOAD_FINISHED))
638 std::vector<Devel::PixelBuffer> pixelBuffers;
639 LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection, loadYuvPlanes, pixelBuffers);
641 if(pixelBuffers.empty())
643 // If pixelBuffer loading is failed in synchronously, call Remove() method.
644 Remove(textureId, nullptr);
645 return INVALID_TEXTURE_ID;
648 if(storageType == StorageType::KEEP_PIXEL_BUFFER) // For the mask image loading.
650 textureInfo.pixelBuffer = pixelBuffers[0]; // Store the pixel data
651 textureInfo.loadState = LoadState::LOAD_FINISHED;
653 else // For the image loading.
656 if(maskTextureId != INVALID_TEXTURE_ID)
658 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
659 if(maskCacheIndex != INVALID_CACHE_INDEX)
661 if(mTextureCacheManager[maskCacheIndex].storageType == StorageType::KEEP_TEXTURE)
663 if(!mTextureCacheManager[maskCacheIndex].textures.empty())
665 maskTexture = mTextureCacheManager[maskCacheIndex].textures[0];
668 else if(mTextureCacheManager[maskCacheIndex].storageType == StorageType::KEEP_PIXEL_BUFFER)
670 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
673 pixelBuffers[0].ApplyMask(maskPixelBuffer, contentScale, cropToMask);
677 DALI_LOG_ERROR("Mask image cached invalid pixel buffer!\n");
683 DALI_LOG_ERROR("Mask image is not stored in cache.\n");
686 PreMultiply(pixelBuffers[0], preMultiplyOnLoad);
689 UploadTextures(pixelBuffers, textureInfo);
697 void TextureManager::Remove(const TextureManager::TextureId& textureId, TextureUploadObserver* observer)
699 if(textureId != INVALID_TEXTURE_ID)
701 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
702 if(textureCacheIndex != INVALID_CACHE_INDEX)
704 TextureManager::TextureId maskTextureId = INVALID_TEXTURE_ID;
705 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
706 // We only need to consider maskTextureId when texture's loadState is not cancelled. Because it is already deleted.
707 if(textureInfo.loadState != LoadState::CANCELLED && textureInfo.loadState != LoadState::MASK_CANCELLED)
709 if(textureInfo.maskTextureId != INVALID_TEXTURE_ID)
711 maskTextureId = textureInfo.maskTextureId;
715 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::Remove( textureId=%d observer=%p ) cacheIndex:%d removal maskTextureId=%d, loadingQueueTextureId=%d, loadState=%s\n", textureId, observer, textureCacheIndex.GetIndex(), maskTextureId, mLoadingQueueTextureId, GET_LOAD_STATE_STRING(textureInfo.loadState));
717 // the case that LoadingQueue is working.
718 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
720 // If textureId is not same, this observer need to delete when ProcessRemoveQueue() is called.
721 // If textureId is same, we should not call RemoveTextureObserver.
722 // Because ObserverDestroyed signal already disconnected in NotifyObservers
723 TextureUploadObserver* queueObserver = observer;
724 if(mLoadingQueueTextureId == textureId)
726 queueObserver = nullptr;
729 // Remove element from the mLoadQueue
730 for(auto&& element : mLoadQueue)
732 if(element.mTextureId == textureId && element.mObserver == observer)
734 // Do not erase the item. We will clear it later in ProcessLoadQueue().
735 element.mTextureId = INVALID_TEXTURE_ID;
736 element.mObserver = nullptr;
741 mRemoveQueue.PushBack(QueueElement(textureId, queueObserver));
745 // Remove its observer
746 RemoveTextureObserver(textureInfo, observer);
748 // Remove textureId in CacheManager. Now, textureInfo is invalidate.
749 mTextureCacheManager.RemoveCache(textureInfo);
751 // Remove maskTextureId in CacheManager
752 if(maskTextureId != INVALID_TEXTURE_ID)
754 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
755 if(maskCacheIndex != INVALID_CACHE_INDEX)
757 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
758 mTextureCacheManager.RemoveCache(maskTextureInfo);
766 void TextureManager::LoadImageSynchronously(
767 const VisualUrl& url,
768 const Dali::ImageDimensions& desiredSize,
769 const Dali::FittingMode::Type& fittingMode,
770 const Dali::SamplingMode::Type& samplingMode,
771 const bool& orientationCorrection,
772 const bool& loadYuvPlanes,
773 std::vector<Devel::PixelBuffer>& pixelBuffers)
775 Devel::PixelBuffer pixelBuffer;
776 if(url.IsBufferResource())
778 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
779 if(encodedImageBuffer)
781 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
788 Dali::LoadImagePlanesFromFile(url.GetUrl(), pixelBuffers, desiredSize, fittingMode, samplingMode, orientationCorrection);
792 pixelBuffer = Dali::LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
798 pixelBuffers.push_back(pixelBuffer);
802 void TextureManager::AddObserver(TextureManager::LifecycleObserver& observer)
804 // make sure an observer doesn't observe the same object twice
805 // otherwise it will get multiple calls to ObjectDestroyed()
806 DALI_ASSERT_DEBUG(mLifecycleObservers.End() == std::find(mLifecycleObservers.Begin(), mLifecycleObservers.End(), &observer));
807 mLifecycleObservers.PushBack(&observer);
810 void TextureManager::RemoveObserver(TextureManager::LifecycleObserver& observer)
812 // Find the observer...
813 auto endIter = mLifecycleObservers.End();
814 for(auto iter = mLifecycleObservers.Begin(); iter != endIter; ++iter)
816 if((*iter) == &observer)
818 mLifecycleObservers.Erase(iter);
822 DALI_ASSERT_DEBUG(endIter != mLifecycleObservers.End());
825 void TextureManager::LoadOrQueueTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
827 switch(textureInfo.loadState)
829 case LoadState::NOT_STARTED:
830 case LoadState::LOAD_FAILED:
832 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
834 QueueLoadTexture(textureInfo, observer);
838 LoadTexture(textureInfo, observer);
842 case LoadState::UPLOADED:
844 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
846 QueueLoadTexture(textureInfo, observer);
850 // The Texture has already loaded. The other observers have already been notified.
851 // We need to send a "late" loaded notification for this observer.
852 EmitLoadComplete(observer, textureInfo, true);
856 case LoadState::LOADING:
857 case LoadState::CANCELLED:
858 case LoadState::MASK_CANCELLED:
859 case LoadState::LOAD_FINISHED:
860 case LoadState::WAITING_FOR_MASK:
861 case LoadState::MASK_APPLYING:
862 case LoadState::MASK_APPLIED:
869 void TextureManager::QueueLoadTexture(const TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
871 const auto& textureId = textureInfo.textureId;
872 mLoadQueue.PushBack(QueueElement(textureId, observer));
876 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
880 void TextureManager::LoadTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
882 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::LoadTexture(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
883 textureInfo.loadState = LoadState::LOADING;
884 if(!textureInfo.loadSynchronously)
886 auto premultiplyOnLoad = (textureInfo.preMultiplyOnLoad && textureInfo.maskTextureId == INVALID_TEXTURE_ID) ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
887 if(textureInfo.animatedImageLoading)
889 mAsyncLoader->LoadAnimatedImage(textureInfo.textureId, textureInfo.animatedImageLoading, textureInfo.frameIndex, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, premultiplyOnLoad);
893 mAsyncLoader->Load(textureInfo.textureId, textureInfo.url, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, textureInfo.orientationCorrection, premultiplyOnLoad, textureInfo.loadYuvPlanes);
896 ObserveTexture(textureInfo, observer);
899 void TextureManager::ProcessLoadQueue()
901 for(auto&& element : mLoadQueue)
903 if(element.mTextureId == INVALID_TEXTURE_ID)
908 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(element.mTextureId);
909 if(cacheIndex != INVALID_CACHE_INDEX)
911 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
912 if((textureInfo.loadState == LoadState::UPLOADED) || (textureInfo.loadState == LoadState::LOAD_FINISHED && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER))
914 if(element.mObserver)
916 EmitLoadComplete(element.mObserver, textureInfo, true);
919 else if(textureInfo.loadState == LoadState::LOADING)
921 // Note : LOADING state texture cannot be queue.
922 // This case be occured when same texture id are queue in mLoadQueue.
923 ObserveTexture(textureInfo, element.mObserver);
927 LoadTexture(textureInfo, element.mObserver);
934 void TextureManager::ProcessRemoveQueue()
936 for(auto&& element : mRemoveQueue)
938 if(element.mTextureId != INVALID_TEXTURE_ID)
940 Remove(element.mTextureId, element.mObserver);
943 mRemoveQueue.Clear();
946 void TextureManager::ObserveTexture(TextureManager::TextureInfo& textureInfo,
947 TextureUploadObserver* observer)
949 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ObserveTexture(): url:%s observer:%p\n", textureInfo.url.GetUrl().c_str(), observer);
953 textureInfo.observerList.PushBack(observer);
954 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
958 void TextureManager::AsyncLoadComplete(const TextureManager::TextureId& textureId, std::vector<Devel::PixelBuffer>& pixelBuffers)
960 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
961 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::AsyncLoadComplete( textureId:%d CacheIndex:%d )\n", textureId, cacheIndex.GetIndex());
962 if(cacheIndex != INVALID_CACHE_INDEX)
964 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
966 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));
967 if(textureInfo.loadState != LoadState::CANCELLED && textureInfo.loadState != LoadState::MASK_CANCELLED)
969 // textureInfo can be invalidated after this call (as the mTextureInfoContainer may be modified)
970 PostLoad(textureInfo, pixelBuffers);
974 Remove(textureInfo.textureId, nullptr);
979 void TextureManager::PostLoad(TextureManager::TextureInfo& textureInfo, std::vector<Devel::PixelBuffer>& pixelBuffers)
981 // Was the load successful?
982 if(!pixelBuffers.empty())
984 if(pixelBuffers.size() == 1)
986 Devel::PixelBuffer pixelBuffer = pixelBuffers[0];
987 if(pixelBuffer && (pixelBuffer.GetWidth() != 0) && (pixelBuffer.GetHeight() != 0))
989 // No atlas support for now
990 textureInfo.useAtlas = UseAtlas::NO_ATLAS;
991 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
993 if(textureInfo.storageType == StorageType::UPLOAD_TO_TEXTURE)
995 // If there is a mask texture ID associated with this texture, then apply the mask
996 // if it's already loaded. If it hasn't, and the mask is still loading,
997 // wait for the mask to finish loading.
998 // note, If the texture is already uploaded synchronously during loading,
999 // we don't need to apply mask.
1000 if(textureInfo.loadState != LoadState::UPLOADED &&
1001 textureInfo.maskTextureId != INVALID_TEXTURE_ID)
1003 if(textureInfo.loadState == LoadState::MASK_APPLYING)
1005 textureInfo.loadState = LoadState::MASK_APPLIED;
1006 UploadTextures(pixelBuffers, textureInfo);
1007 NotifyObservers(textureInfo, true);
1011 LoadState maskLoadState = mTextureCacheManager.GetTextureStateInternal(textureInfo.maskTextureId);
1012 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
1013 if(maskLoadState == LoadState::LOADING)
1015 textureInfo.loadState = LoadState::WAITING_FOR_MASK;
1017 else if(maskLoadState == LoadState::LOAD_FINISHED || maskLoadState == LoadState::UPLOADED)
1019 // Send New Task to Thread
1020 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureInfo.maskTextureId);
1021 if(maskCacheIndex != INVALID_CACHE_INDEX)
1023 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
1024 if(maskTextureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER)
1026 // Send New Task to Thread
1027 ApplyMask(textureInfo, textureInfo.maskTextureId);
1029 else if(maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1031 // Upload image texture. textureInfo.loadState will be UPLOADED.
1032 UploadTextures(pixelBuffers, textureInfo);
1034 // notify mask texture set.
1035 NotifyObservers(textureInfo, true);
1039 else // maskLoadState == LoadState::LOAD_FAILED
1041 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1042 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1043 UploadTextures(pixelBuffers, textureInfo);
1044 NotifyObservers(textureInfo, true);
1050 UploadTextures(pixelBuffers, textureInfo);
1051 NotifyObservers(textureInfo, true);
1056 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
1057 textureInfo.loadState = LoadState::LOAD_FINISHED;
1059 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1061 NotifyObservers(textureInfo, true);
1063 else // for the StorageType::KEEP_PIXEL_BUFFER and StorageType::KEEP_TEXTURE
1065 // Check if there was another texture waiting for this load to complete
1066 // (e.g. if this was an image mask, and its load is on a different thread)
1067 CheckForWaitingTexture(textureInfo);
1075 // No atlas support for now
1076 textureInfo.useAtlas = UseAtlas::NO_ATLAS;
1077 textureInfo.preMultiplied = false;
1079 UploadTextures(pixelBuffers, textureInfo);
1080 NotifyObservers(textureInfo, true);
1085 textureInfo.loadState = LoadState::LOAD_FAILED;
1086 if(textureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER || textureInfo.storageType == StorageType::KEEP_TEXTURE)
1088 // Check if there was another texture waiting for this load to complete
1089 // (e.g. if this was an image mask, and its load is on a different thread)
1090 CheckForWaitingTexture(textureInfo);
1094 NotifyObservers(textureInfo, false);
1099 void TextureManager::CheckForWaitingTexture(TextureManager::TextureInfo& maskTextureInfo)
1101 if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED &&
1102 maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1104 // Upload mask texture. textureInfo.loadState will be UPLOADED.
1105 std::vector<Devel::PixelBuffer> pixelBuffers;
1106 pixelBuffers.push_back(maskTextureInfo.pixelBuffer);
1107 UploadTextures(pixelBuffers, maskTextureInfo);
1110 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): maskTextureId=%d, maskTextureUrl=%s\n", maskTextureInfo.textureId, maskTextureInfo.url.GetUrl().c_str());
1112 // Search the cache, checking if any texture has this texture id as a maskTextureId
1113 const std::size_t size = mTextureCacheManager.size();
1115 // Keep notify observer required textureIds.
1116 // Note : NotifyObservers can change mTextureCacheManager cache struct. We should check id's validation before notify.
1117 std::vector<TextureId> notifyRequiredTextureIds;
1119 // TODO : Refactorize here to not iterate whole cached image.
1120 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1122 if(mTextureCacheManager[cacheIndex].maskTextureId == maskTextureInfo.textureId &&
1123 mTextureCacheManager[cacheIndex].loadState == LoadState::WAITING_FOR_MASK)
1125 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1127 if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED)
1129 if(maskTextureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER)
1131 // Send New Task to Thread
1132 ApplyMask(textureInfo, maskTextureInfo.textureId);
1135 else if(maskTextureInfo.loadState == LoadState::UPLOADED)
1137 if(maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1139 // Upload image texture. textureInfo.loadState will be UPLOADED.
1140 std::vector<Devel::PixelBuffer> pixelBuffers;
1141 pixelBuffers.push_back(textureInfo.pixelBuffer);
1142 UploadTextures(pixelBuffers, textureInfo);
1144 // Increase reference counts for notify required textureId.
1145 // Now we can assume that we don't remove & re-assign this textureId
1146 // during NotifyObserver signal emit.
1147 maskTextureInfo.referenceCount++;
1148 textureInfo.referenceCount++;
1150 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): Ready to notify textureId=%d\n", textureInfo.textureId);
1152 notifyRequiredTextureIds.push_back(textureInfo.textureId);
1155 else // maskTextureInfo.loadState == LoadState::LOAD_FAILED
1157 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1158 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1159 std::vector<Devel::PixelBuffer> pixelBuffers;
1160 pixelBuffers.push_back(textureInfo.pixelBuffer);
1161 UploadTextures(pixelBuffers, textureInfo);
1163 // Increase reference counts for notify required textureId.
1164 // Now we can assume that we don't remove & re-assign this textureId
1165 // during NotifyObserver signal emit.
1166 maskTextureInfo.referenceCount++;
1167 textureInfo.referenceCount++;
1169 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): Ready to notify textureId=%d\n", textureInfo.textureId);
1171 notifyRequiredTextureIds.push_back(textureInfo.textureId);
1176 // Notify textures are masked
1177 for(const auto textureId : notifyRequiredTextureIds)
1179 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1180 if(textureCacheIndex != INVALID_CACHE_INDEX)
1182 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
1183 NotifyObservers(textureInfo, true);
1187 // Decrease reference count
1188 for(const auto textureId : notifyRequiredTextureIds)
1190 Remove(textureId, nullptr);
1194 void TextureManager::ApplyMask(TextureManager::TextureInfo& textureInfo, const TextureManager::TextureId& maskTextureId)
1196 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
1197 if(maskCacheIndex != INVALID_CACHE_INDEX)
1199 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
1200 Devel::PixelBuffer pixelBuffer = textureInfo.pixelBuffer;
1201 textureInfo.pixelBuffer.Reset();
1203 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ApplyMask(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
1205 textureInfo.loadState = LoadState::MASK_APPLYING;
1206 auto premultiplyOnLoad = textureInfo.preMultiplyOnLoad ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
1207 mAsyncLoader->ApplyMask(textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask, premultiplyOnLoad);
1211 void TextureManager::UploadTextures(std::vector<Devel::PixelBuffer>& pixelBuffers, TextureManager::TextureInfo& textureInfo)
1213 if(!pixelBuffers.empty() && textureInfo.loadState != LoadState::UPLOADED && textureInfo.useAtlas != UseAtlas::USE_ATLAS)
1215 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, " TextureManager::UploadTextures() New Texture for textureId:%d\n", textureInfo.textureId);
1217 // Check if this pixelBuffer is premultiplied
1218 textureInfo.preMultiplied = pixelBuffers[0].IsAlphaPreMultiplied();
1220 auto& renderingAddOn = RenderingAddOn::Get();
1221 if(renderingAddOn.IsValid())
1223 renderingAddOn.CreateGeometry(textureInfo.textureId, pixelBuffers[0]);
1226 // Remove previous textures and insert new textures
1227 textureInfo.textures.clear();
1229 for(auto&& pixelBuffer : pixelBuffers)
1231 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight());
1232 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
1233 texture.Upload(pixelData);
1234 textureInfo.textures.push_back(texture);
1238 // Update the load state.
1239 // Note: This is regardless of success as we care about whether a
1240 // load attempt is in progress or not. If unsuccessful, a broken
1241 // image is still loaded.
1242 textureInfo.loadState = LoadState::UPLOADED;
1245 void TextureManager::NotifyObservers(TextureManager::TextureInfo& textureInfo, const bool& success)
1247 TextureId textureId = textureInfo.textureId;
1249 // If there is an observer: Notify the load is complete, whether successful or not,
1250 // and erase it from the list
1251 TextureInfo* info = &textureInfo;
1253 if(info->animatedImageLoading)
1255 // If loading failed, we don't need to get frameCount and frameInterval.
1258 info->frameCount = info->animatedImageLoading.GetImageCount();
1259 info->frameInterval = info->animatedImageLoading.GetFrameInterval(info->frameIndex);
1261 info->animatedImageLoading.Reset();
1264 mLoadingQueueTextureId = textureId;
1266 // Reverse observer list that we can pop_back the observer.
1267 std::reverse(info->observerList.Begin(), info->observerList.End());
1269 while(info->observerList.Count())
1271 TextureUploadObserver* observer = *(info->observerList.End() - 1u);
1273 // During LoadComplete() a Control ResourceReady() signal is emitted.
1274 // During that signal the app may add remove /add Textures (e.g. via
1276 // It is possible for observers to be removed from the observer list,
1277 // and it is also possible for the mTextureInfoContainer to be modified,
1278 // invalidating the reference to the textureInfo struct.
1279 // Texture load requests for the same URL are deferred until the end of this
1281 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));
1282 // It is possible for the observer to be deleted.
1283 // Disconnect and remove the observer first.
1284 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1286 info->observerList.Erase(info->observerList.End() - 1u);
1288 EmitLoadComplete(observer, *info, success);
1290 // Get the textureInfo from the container again as it may have been invalidated.
1291 TextureCacheIndex textureInfoIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1292 if(textureInfoIndex == INVALID_CACHE_INDEX)
1294 break; // texture has been removed - can stop.
1296 info = &mTextureCacheManager[textureInfoIndex];
1299 mLoadingQueueTextureId = INVALID_TEXTURE_ID;
1301 ProcessRemoveQueue();
1303 if(info->storageType == StorageType::RETURN_PIXEL_BUFFER && info->observerList.Count() == 0)
1305 Remove(info->textureId, nullptr);
1309 void TextureManager::ObserverDestroyed(TextureUploadObserver* observer)
1311 const std::size_t size = mTextureCacheManager.size();
1312 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1314 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1315 for(TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin();
1316 j != textureInfo.observerList.End();)
1320 j = textureInfo.observerList.Erase(j);
1329 // Remove element from the LoadQueue
1330 for(auto&& element : mLoadQueue)
1332 if(element.mObserver == observer)
1334 element.mTextureId = INVALID_TEXTURE_ID;
1335 element.mObserver = nullptr;
1340 Dali::Geometry TextureManager::GetRenderGeometry(const TextureManager::TextureId& textureId, std::uint32_t& frontElements, std::uint32_t& backElements)
1342 return RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().GetGeometry(textureId, frontElements, backElements) : Geometry();
1345 void TextureManager::EmitLoadComplete(TextureUploadObserver* observer, TextureManager::TextureInfo& textureInfo, const bool& success)
1347 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1349 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::PIXEL_BUFFER, textureInfo.pixelBuffer, textureInfo.url.GetUrl(), textureInfo.preMultiplied));
1353 TextureSet textureSet = GetTextureSet(textureInfo);
1354 if(textureInfo.isAnimatedImageFormat)
1356 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::ANIMATED_IMAGE_TEXTURE, textureInfo.textureId, textureSet, textureInfo.frameCount, textureInfo.frameInterval));
1360 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::TEXTURE, textureInfo.textureId, textureSet, (textureInfo.useAtlas == UseAtlas::USE_ATLAS) ? true : false, textureInfo.atlasRect, textureInfo.preMultiplied));
1365 TextureSet TextureManager::GetTextureSet(const TextureManager::TextureId& textureId)
1367 TextureSet textureSet;
1368 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
1369 if(loadState == TextureManager::LoadState::UPLOADED)
1371 // LoadComplete has already been called - keep the same texture set
1372 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1373 if(textureCacheIndex != INVALID_CACHE_INDEX)
1375 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
1376 textureSet = GetTextureSet(textureInfo);
1381 DALI_LOG_ERROR("GetTextureSet is failed. texture is not uploaded \n");
1386 TextureSet TextureManager::GetTextureSet(const TextureManager::TextureInfo& textureInfo)
1388 TextureSet textureSet;
1390 // LoadComplete has already been called - keep the same texture set
1391 textureSet = TextureSet::New();
1392 if(!textureInfo.textures.empty())
1394 if(textureInfo.textures.size() > 1) // For YUV case
1396 uint32_t index = 0u;
1397 for(auto&& texture : textureInfo.textures)
1399 textureSet.SetTexture(index++, texture);
1404 textureSet.SetTexture(TEXTURE_INDEX, textureInfo.textures[0]);
1405 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureInfo.maskTextureId);
1406 if(maskCacheIndex != INVALID_CACHE_INDEX)
1408 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
1409 if(maskTextureInfo.storageType == StorageType::UPLOAD_TO_TEXTURE || maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1411 if(!maskTextureInfo.textures.empty())
1413 textureSet.SetTexture(MASK_TEXTURE_INDEX, maskTextureInfo.textures[0]);
1422 void TextureManager::RemoveTextureObserver(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
1424 // Remove its observer
1427 const auto iterEnd = textureInfo.observerList.End();
1428 const auto iter = std::find(textureInfo.observerList.Begin(), iterEnd, observer);
1431 // Disconnect and remove the observer.
1432 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1433 textureInfo.observerList.Erase(iter);
1438 } // namespace Internal
1440 } // namespace Toolkit