2 * Copyright (c) 2024 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/image-loading.h>
23 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
24 #include <dali/integration-api/adaptor-framework/adaptor.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/integration-api/trace.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 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_IMAGE_PERFORMANCE_MARKER, false);
52 // Define logfilter Internal namespace level so other files (e.g. texture-cache-manager.cpp) can also use this filter.
53 Debug::Filter* gTextureManagerLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TEXTURE_MANAGER");
56 #define GET_LOAD_STATE_STRING(loadState) \
57 loadState == TextureManagerType::LoadState::NOT_STARTED ? "NOT_STARTED" : \
58 loadState == TextureManagerType::LoadState::LOADING ? "LOADING" : \
59 loadState == TextureManagerType::LoadState::LOAD_FINISHED ? "LOAD_FINISHED" : \
60 loadState == TextureManagerType::LoadState::WAITING_FOR_MASK ? "WAITING_FOR_MASK" : \
61 loadState == TextureManagerType::LoadState::MASK_APPLYING ? "MASK_APPLYING" : \
62 loadState == TextureManagerType::LoadState::MASK_APPLIED ? "MASK_APPLIED" : \
63 loadState == TextureManagerType::LoadState::UPLOADED ? "UPLOADED" : \
64 loadState == TextureManagerType::LoadState::CANCELLED ? "CANCELLED" : \
65 loadState == TextureManagerType::LoadState::MASK_CANCELLED ? "MASK_CANCELLED" : \
66 loadState == TextureManagerType::LoadState::LOAD_FAILED ? "LOAD_FAILED" : \
73 const Vector4 FULL_ATLAS_RECT(0.0f, 0.0f, 1.0f, 1.0f); ///< UV Rectangle that covers the full Texture
75 void PreMultiply(Devel::PixelBuffer pixelBuffer, TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
77 if(Pixel::HasAlpha(pixelBuffer.GetPixelFormat()))
79 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
81 pixelBuffer.MultiplyColorByAlpha();
86 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
90 } // Anonymous namespace
92 TextureManager::MaskingData::MaskingData()
94 mAlphaMaskId(INVALID_TEXTURE_ID),
95 mContentScaleFactor(1.0f),
97 mPreappliedMasking(true),
98 mMaskImageLoadingFailed(false)
102 TextureManager::TextureManager(bool loadYuvPlanes)
103 : mTextureCacheManager(),
104 mAsyncLoader(std::unique_ptr<TextureAsyncLoadingHelper>(new TextureAsyncLoadingHelper(*this))),
106 mLoadingQueueTextureId(INVALID_TEXTURE_ID),
108 mLoadYuvPlanes(loadYuvPlanes),
109 mRemoveProcessorRegistered(false)
111 // Initialize the AddOn
112 RenderingAddOn::Get();
115 TextureManager::~TextureManager()
117 if(mRemoveProcessorRegistered && Adaptor::IsAvailable())
119 Adaptor::Get().UnregisterProcessor(*this, true);
120 mRemoveProcessorRegistered = false;
124 TextureSet TextureManager::LoadAnimatedImageTexture(
125 const VisualUrl& url,
126 Dali::AnimatedImageLoading animatedImageLoading,
127 const uint32_t frameIndex,
128 TextureManager::TextureId& textureId,
129 MaskingDataPointer& maskInfo,
130 const Dali::ImageDimensions& desiredSize,
131 const Dali::FittingMode::Type fittingMode,
132 const Dali::SamplingMode::Type samplingMode,
133 const bool synchronousLoading,
134 TextureUploadObserver* textureObserver,
135 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
137 TextureSet textureSet;
139 if(synchronousLoading)
141 Devel::PixelBuffer pixelBuffer;
142 if(animatedImageLoading)
144 pixelBuffer = animatedImageLoading.LoadFrame(frameIndex, desiredSize, fittingMode, samplingMode);
148 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous loading is failed\n");
153 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
155 Devel::PixelBuffer maskPixelBuffer = LoadImageFromFile(maskInfo->mAlphaMaskUrl.GetUrl(), desiredSize, fittingMode, samplingMode, true);
158 if(!maskInfo->mPreappliedMasking)
160 PixelData maskPixelData = Devel::PixelBuffer::Convert(maskPixelBuffer); // takes ownership of buffer
161 maskTexture = Texture::New(Dali::TextureType::TEXTURE_2D, maskPixelData.GetPixelFormat(), maskPixelData.GetWidth(), maskPixelData.GetHeight());
162 maskTexture.Upload(maskPixelData);
166 pixelBuffer.ApplyMask(maskPixelBuffer, maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
171 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous mask image loading is failed\n");
175 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
177 PreMultiply(pixelBuffer, preMultiplyOnLoad);
180 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
183 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight());
184 texture.Upload(pixelData);
185 textureSet = TextureSet::New();
186 textureSet.SetTexture(TEXTURE_INDEX, texture);
189 textureSet.SetTexture(MASK_TEXTURE_INDEX, maskTexture);
196 TextureId alphaMaskId = INVALID_TEXTURE_ID;
197 float contentScaleFactor = 1.0f;
198 bool cropToMask = false;
199 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
201 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, maskInfo->mPreappliedMasking ? TextureManager::StorageType::KEEP_PIXEL_BUFFER : TextureManager::StorageType::KEEP_TEXTURE);
202 alphaMaskId = maskInfo->mAlphaMaskId;
203 if(maskInfo->mPreappliedMasking)
205 contentScaleFactor = maskInfo->mContentScaleFactor;
206 cropToMask = maskInfo->mCropToMask;
210 textureId = RequestLoadInternal(url, alphaMaskId, textureId, contentScaleFactor, desiredSize, fittingMode, samplingMode, cropToMask, TextureManager::StorageType::UPLOAD_TO_TEXTURE, textureObserver, true, TextureManager::ReloadPolicy::CACHED, preMultiplyOnLoad, animatedImageLoading, frameIndex, false);
212 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
213 if(loadState == TextureManager::LoadState::UPLOADED)
215 // LoadComplete has already been called - keep the same texture set
216 textureSet = GetTextureSet(textureId);
223 Devel::PixelBuffer TextureManager::LoadPixelBuffer(
224 const VisualUrl& url,
225 const Dali::ImageDimensions& desiredSize,
226 const Dali::FittingMode::Type fittingMode,
227 const Dali::SamplingMode::Type samplingMode,
228 const bool synchronousLoading,
229 TextureUploadObserver* textureObserver,
230 const bool orientationCorrection,
231 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
233 Devel::PixelBuffer pixelBuffer;
234 if(synchronousLoading)
238 if(url.IsBufferResource())
240 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
241 if(encodedImageBuffer)
243 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
248 pixelBuffer = LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
250 if(pixelBuffer && preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
252 PreMultiply(pixelBuffer, preMultiplyOnLoad);
258 RequestLoadInternal(url, INVALID_TEXTURE_ID, INVALID_TEXTURE_ID, 1.0f, desiredSize, fittingMode, samplingMode, false, TextureManager::StorageType::RETURN_PIXEL_BUFFER, textureObserver, orientationCorrection, TextureManager::ReloadPolicy::FORCED, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, false);
264 TextureSet TextureManager::LoadTexture(
265 const VisualUrl& url,
266 const Dali::ImageDimensions& desiredSize,
267 const Dali::FittingMode::Type fittingMode,
268 const Dali::SamplingMode::Type samplingMode,
269 MaskingDataPointer& maskInfo,
270 const bool synchronousLoading,
271 TextureManager::TextureId& textureId,
272 Vector4& textureRect,
273 Dali::ImageDimensions& textureRectSize,
274 bool& atlasingStatus,
276 TextureUploadObserver* textureObserver,
277 AtlasUploadObserver* atlasObserver,
278 ImageAtlasManagerPtr imageAtlasManager,
279 const bool orientationCorrection,
280 const TextureManager::ReloadPolicy reloadPolicy,
281 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
283 TextureSet textureSet;
285 loadingStatus = false;
286 textureRect = FULL_ATLAS_RECT;
288 if(VisualUrl::TEXTURE == url.GetProtocolType())
290 std::string location = url.GetLocation();
291 if(location.size() > 0u)
293 TextureId id = std::stoi(location);
294 auto externalTextureInfo = mTextureCacheManager.GetExternalTextureInfo(id);
295 if(externalTextureInfo.textureSet)
297 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
299 // Change preMultiplyOnLoad value so make caller determine to preMultiplyAlpha or not.
300 // TODO : Should we seperate input and output value?
301 preMultiplyOnLoad = externalTextureInfo.preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
304 TextureId alphaMaskId = INVALID_TEXTURE_ID;
305 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
307 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, TextureManager::StorageType::KEEP_TEXTURE, synchronousLoading);
308 alphaMaskId = maskInfo->mAlphaMaskId;
310 // Create new textureId. this textureId is not same as location
311 textureId = RequestLoad(url, alphaMaskId, textureId, 1.0f, desiredSize, fittingMode, samplingMode, false, textureObserver, orientationCorrection, reloadPolicy, preMultiplyOnLoad, synchronousLoading);
313 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
314 if(loadState == TextureManager::LoadState::UPLOADED)
316 textureSet = GetTextureSet(textureId);
321 // TextureId is same as location
324 textureSet = TextureSet::New();
325 textureSet.SetTexture(TEXTURE_INDEX, externalTextureInfo.textureSet.GetTexture(TEXTURE_INDEX));
333 if(synchronousLoading && atlasingStatus)
335 const bool synchronousAtlasAvaliable = (desiredSize != ImageDimensions() || url.IsLocalResource()) ? imageAtlasManager->CheckAtlasAvailable(url, desiredSize)
337 if(synchronousAtlasAvaliable)
339 std::vector<Devel::PixelBuffer> pixelBuffers;
340 LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection, false, pixelBuffers);
342 if(!pixelBuffers.empty() && maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
344 std::vector<Devel::PixelBuffer> maskPixelBuffers;
345 LoadImageSynchronously(maskInfo->mAlphaMaskUrl, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true, false, maskPixelBuffers);
346 if(!maskPixelBuffers.empty())
348 pixelBuffers[0].ApplyMask(maskPixelBuffers[0], maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
353 if(!pixelBuffers.empty())
355 PreMultiply(pixelBuffers[0], preMultiplyOnLoad);
356 data = Devel::PixelBuffer::Convert(pixelBuffers[0]); // takes ownership of buffer
360 textureSet = imageAtlasManager->Add(textureRect, data);
363 textureRectSize.SetWidth(data.GetWidth());
364 textureRectSize.SetHeight(data.GetHeight());
369 DALI_LOG_ERROR("TextureManager::LoadTexture: Synchronous Texture loading with atlasing is failed.\n");
374 atlasingStatus = false;
381 loadingStatus = true;
382 // Atlas manager can chage desired size when it is set by 0,0.
383 // We should store into textureRectSize only if atlasing successed.
384 // So copy inputed desiredSize, and replace value into textureRectSize only if atlasing success.
385 Dali::ImageDimensions atlasDesiredSize = desiredSize;
388 if(url.IsBufferResource())
390 const EncodedImageBuffer& encodedImageBuffer = GetEncodedImageBuffer(url.GetUrl());
391 if(encodedImageBuffer)
393 textureSet = imageAtlasManager->Add(textureRect, encodedImageBuffer, desiredSize, fittingMode, true, atlasObserver);
398 textureSet = imageAtlasManager->Add(textureRect, url, atlasDesiredSize, fittingMode, true, atlasObserver);
401 if(!textureSet) // big image, no atlasing or atlasing failed
403 atlasingStatus = false;
405 TextureId alphaMaskId = INVALID_TEXTURE_ID;
406 float contentScaleFactor = 1.0f;
407 bool cropToMask = false;
408 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
410 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, maskInfo->mPreappliedMasking ? TextureManager::StorageType::KEEP_PIXEL_BUFFER : TextureManager::StorageType::KEEP_TEXTURE, synchronousLoading);
411 alphaMaskId = maskInfo->mAlphaMaskId;
412 if(maskInfo && maskInfo->mPreappliedMasking)
414 contentScaleFactor = maskInfo->mContentScaleFactor;
415 cropToMask = maskInfo->mCropToMask;
419 textureId = RequestLoad(
429 orientationCorrection,
434 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
435 if(loadState == TextureManager::LoadState::UPLOADED)
437 // LoadComplete has already been called - keep the same texture set
438 textureSet = GetTextureSet(textureId);
441 // If we are loading the texture, or waiting for the ready signal handler to complete, inform
442 // caller that they need to wait.
443 loadingStatus = (loadState == TextureManager::LoadState::LOADING ||
444 loadState == TextureManager::LoadState::WAITING_FOR_MASK ||
445 loadState == TextureManager::LoadState::MASK_APPLYING ||
446 loadState == TextureManager::LoadState::MASK_APPLIED ||
447 loadState == TextureManager::LoadState::NOT_STARTED ||
448 mLoadingQueueTextureId != INVALID_TEXTURE_ID);
452 textureRectSize = atlasDesiredSize;
457 if(synchronousLoading)
459 loadingStatus = false;
465 TextureManager::TextureId TextureManager::RequestLoad(
466 const VisualUrl& url,
467 const ImageDimensions& desiredSize,
468 const Dali::FittingMode::Type fittingMode,
469 const Dali::SamplingMode::Type samplingMode,
470 TextureUploadObserver* observer,
471 const bool orientationCorrection,
472 const TextureManager::ReloadPolicy reloadPolicy,
473 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
474 const bool synchronousLoading)
476 return RequestLoadInternal(url, INVALID_TEXTURE_ID, INVALID_TEXTURE_ID, 1.0f, desiredSize, fittingMode, samplingMode, false, TextureManager::StorageType::UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
479 TextureManager::TextureId TextureManager::RequestLoad(
480 const VisualUrl& url,
481 const TextureManager::TextureId maskTextureId,
482 const TextureManager::TextureId previousTextureId,
483 const float contentScale,
484 const Dali::ImageDimensions& desiredSize,
485 const Dali::FittingMode::Type fittingMode,
486 const Dali::SamplingMode::Type samplingMode,
487 const bool cropToMask,
488 TextureUploadObserver* observer,
489 const bool orientationCorrection,
490 const TextureManager::ReloadPolicy reloadPolicy,
491 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
492 const bool synchronousLoading)
494 return RequestLoadInternal(url, maskTextureId, previousTextureId, contentScale, desiredSize, fittingMode, samplingMode, cropToMask, TextureManager::StorageType::UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
497 TextureManager::TextureId TextureManager::RequestMaskLoad(
498 const VisualUrl& maskUrl,
499 const TextureManager::StorageType storageType,
500 const bool synchronousLoading)
502 // Use the normal load procedure to get the alpha mask.
503 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
504 return RequestLoadInternal(maskUrl, INVALID_TEXTURE_ID, INVALID_TEXTURE_ID, 1.0f, ImageDimensions(), Dali::FittingMode::SCALE_TO_FILL, Dali::SamplingMode::NO_FILTER, false, storageType, NULL, true, TextureManager::ReloadPolicy::CACHED, preMultiply, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
507 TextureManager::TextureId TextureManager::RequestLoadInternal(
508 const VisualUrl& url,
509 const TextureManager::TextureId maskTextureId,
510 const TextureManager::TextureId previousTextureId,
511 const float contentScale,
512 const Dali::ImageDimensions& desiredSize,
513 const Dali::FittingMode::Type fittingMode,
514 const Dali::SamplingMode::Type samplingMode,
515 const bool cropToMask,
516 const TextureManager::StorageType storageType,
517 TextureUploadObserver* observer,
518 const bool orientationCorrection,
519 const TextureManager::ReloadPolicy reloadPolicy,
520 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
521 Dali::AnimatedImageLoading animatedImageLoading,
522 const uint32_t frameIndex,
523 const bool synchronousLoading)
525 TextureHash textureHash = INITIAL_HASH_NUMBER;
526 TextureCacheIndex cacheIndex = INVALID_CACHE_INDEX;
527 bool loadYuvPlanes = (mLoadYuvPlanes && maskTextureId == INVALID_TEXTURE_ID && storageType == TextureManager::StorageType::UPLOAD_TO_TEXTURE);
529 if(storageType != TextureManager::StorageType::RETURN_PIXEL_BUFFER)
531 textureHash = mTextureCacheManager.GenerateHash(url, desiredSize, fittingMode, samplingMode, maskTextureId, cropToMask, frameIndex);
533 // Look up the texture by hash. Note: The extra parameters are used in case of a hash collision.
534 cacheIndex = mTextureCacheManager.FindCachedTexture(textureHash, url, desiredSize, fittingMode, samplingMode, storageType, maskTextureId, cropToMask, preMultiplyOnLoad, (animatedImageLoading) ? true : false, frameIndex);
537 TextureManager::TextureId textureId = INVALID_TEXTURE_ID;
538 // Check if the requested Texture exists in the cache.
539 if(cacheIndex != INVALID_CACHE_INDEX)
541 if(TextureManager::ReloadPolicy::CACHED == reloadPolicy || TextureManager::INVALID_TEXTURE_ID == previousTextureId)
543 // Mark this texture being used by another client resource, or Reload forced without request load before.
544 // Forced reload which have current texture before, would replace the current texture.
545 // without the need for incrementing the reference count.
546 ++(mTextureCacheManager[cacheIndex].referenceCount);
548 textureId = mTextureCacheManager[cacheIndex].textureId;
550 // Update preMultiplyOnLoad value. It should be changed according to preMultiplied value of the cached info.
551 preMultiplyOnLoad = mTextureCacheManager[cacheIndex].preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
552 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::RequestLoad( url=%s observer=%p ) Using cached texture id@%d, textureId=%d, maskTextureId=%d, prevTextureId=%d, frameindex=%d, premultiplied=%d, refCount=%d\n", url.GetUrl().c_str(), observer, cacheIndex.GetIndex(), textureId, maskTextureId, previousTextureId, frameIndex, mTextureCacheManager[cacheIndex].preMultiplied ? 1 : 0, static_cast<int>(mTextureCacheManager[cacheIndex].referenceCount));
555 if(textureId == INVALID_TEXTURE_ID) // There was no caching, or caching not required
557 textureId = mTextureCacheManager.GenerateTextureId();
559 bool preMultiply = (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
561 // Cache new texutre, and get cacheIndex.
562 cacheIndex = mTextureCacheManager.AppendCache(TextureInfo(textureId, maskTextureId, url, desiredSize, contentScale, fittingMode, samplingMode, false, cropToMask, textureHash, orientationCorrection, preMultiply, animatedImageLoading, frameIndex, loadYuvPlanes));
563 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);
566 // The below code path is common whether we are using the cache or not.
567 // The textureInfoIndex now refers to either a pre-existing cached TextureInfo,
568 // or a new TextureInfo just created.
569 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
570 textureInfo.maskTextureId = maskTextureId;
571 textureInfo.storageType = storageType;
572 textureInfo.orientationCorrection = orientationCorrection;
574 // the case using external texture has already been loaded texture, so change its status to WAITING_FOR_MASK.
575 if(url.GetProtocolType() == VisualUrl::TEXTURE)
577 if(textureInfo.loadState != TextureManager::LoadState::UPLOADED)
579 textureInfo.preMultiplied = (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
580 textureInfo.loadState = TextureManager::LoadState::WAITING_FOR_MASK;
584 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureInfo loadState:%s\n", GET_LOAD_STATE_STRING(textureInfo.loadState));
586 // Force reloading of texture by setting loadState unless already loading or cancelled.
587 if(TextureManager::ReloadPolicy::FORCED == reloadPolicy &&
588 TextureManager::LoadState::LOADING != textureInfo.loadState &&
589 TextureManager::LoadState::WAITING_FOR_MASK != textureInfo.loadState &&
590 TextureManager::LoadState::MASK_APPLYING != textureInfo.loadState &&
591 TextureManager::LoadState::MASK_APPLIED != textureInfo.loadState &&
592 TextureManager::LoadState::CANCELLED != textureInfo.loadState &&
593 TextureManager::LoadState::MASK_CANCELLED != textureInfo.loadState)
595 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, "TextureManager::RequestLoad( url=%s observer=%p ) ForcedReload cacheIndex:%d, textureId=%d, maskTextureId=%d, prevTextureId=%d\n", url.GetUrl().c_str(), observer, cacheIndex.GetIndex(), textureId, maskTextureId, previousTextureId);
596 textureInfo.loadState = TextureManager::LoadState::NOT_STARTED;
599 if(!synchronousLoading)
601 // Check if we should add the observer.
602 // Only do this if we have not loaded yet and it will not have loaded by the end of this method.
603 switch(textureInfo.loadState)
605 case TextureManager::LoadState::LOAD_FAILED: // Failed notifies observer which then stops observing.
606 case TextureManager::LoadState::NOT_STARTED:
608 LoadOrQueueTexture(textureInfo, observer); // If called inside NotifyObservers, queues until afterwards
611 case TextureManager::LoadState::LOADING:
612 case TextureManager::LoadState::WAITING_FOR_MASK:
613 case TextureManager::LoadState::MASK_APPLYING:
614 case TextureManager::LoadState::MASK_APPLIED:
616 // Do not observe even we reload forced when texture is already loading state.
617 if(TextureManager::ReloadPolicy::CACHED == reloadPolicy || TextureManager::INVALID_TEXTURE_ID == previousTextureId)
619 ObserveTexture(textureInfo, observer);
623 case TextureManager::LoadState::UPLOADED:
627 LoadOrQueueTexture(textureInfo, observer);
631 case TextureManager::LoadState::CANCELLED:
633 // A cancelled texture hasn't finished loading yet. Treat as a loading texture
634 // (it's ref count has already been incremented, above)
635 textureInfo.loadState = TextureManager::LoadState::LOADING;
636 ObserveTexture(textureInfo, observer);
639 case TextureManager::LoadState::MASK_CANCELLED:
641 // A cancelled texture hasn't finished mask applying yet. Treat as a mask applying texture
642 // (it's ref count has already been incremented, above)
643 textureInfo.loadState = TextureManager::LoadState::MASK_APPLYING;
644 ObserveTexture(textureInfo, observer);
647 case TextureManager::LoadState::LOAD_FINISHED:
649 // Loading has already completed.
650 if(observer && textureInfo.storageType == TextureManager::StorageType::RETURN_PIXEL_BUFFER)
652 LoadOrQueueTexture(textureInfo, observer);
660 // If the image is already finished to load, use cached texture.
661 // We don't need to consider Observer because this is synchronous loading.
662 if(!(textureInfo.loadState == TextureManager::LoadState::UPLOADED ||
663 textureInfo.loadState == TextureManager::LoadState::LOAD_FINISHED))
665 if(url.GetProtocolType() == VisualUrl::TEXTURE)
667 // Get external textureSet from cacheManager.
668 std::string location = textureInfo.url.GetLocation();
669 if(!location.empty())
671 TextureId id = std::stoi(location);
672 auto externalTextureInfo = mTextureCacheManager.GetExternalTextureInfo(id);
673 textureInfo.textures.push_back(externalTextureInfo.textureSet.GetTexture(0));
674 textureInfo.loadState = TextureManager::LoadState::UPLOADED;
679 std::vector<Devel::PixelBuffer> pixelBuffers;
680 LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection, loadYuvPlanes, pixelBuffers);
682 if(pixelBuffers.empty())
684 // If pixelBuffer loading is failed in synchronously, call RequestRemove() method.
685 RequestRemove(textureId, nullptr);
686 return INVALID_TEXTURE_ID;
689 if(storageType == TextureManager::StorageType::KEEP_PIXEL_BUFFER) // For the mask image loading.
691 textureInfo.pixelBuffer = pixelBuffers[0]; // Store the pixel data
692 textureInfo.loadState = TextureManager::LoadState::LOAD_FINISHED;
694 else // For the image loading.
697 if(maskTextureId != INVALID_TEXTURE_ID)
699 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
700 if(maskCacheIndex != INVALID_CACHE_INDEX)
702 if(mTextureCacheManager[maskCacheIndex].storageType == TextureManager::StorageType::KEEP_PIXEL_BUFFER)
704 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
707 pixelBuffers[0].ApplyMask(maskPixelBuffer, contentScale, cropToMask);
711 DALI_LOG_ERROR("Mask image cached invalid pixel buffer!\n");
717 DALI_LOG_ERROR("Mask image is not stored in cache.\n");
720 PreMultiply(pixelBuffers[0], preMultiplyOnLoad);
723 UploadTextures(pixelBuffers, textureInfo);
732 void TextureManager::RequestRemove(const TextureManager::TextureId textureId, TextureUploadObserver* observer)
734 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::RequestRemove( textureId=%d observer=%p )\n", textureId, observer);
737 if(textureId != INVALID_TEXTURE_ID)
739 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
740 if(textureCacheIndex != INVALID_CACHE_INDEX)
744 // Remove observer from cached texture info
745 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
746 RemoveTextureObserver(textureInfo, observer);
749 mRemoveQueue.PushBack(textureId);
751 if(!mRemoveProcessorRegistered && Adaptor::IsAvailable())
753 mRemoveProcessorRegistered = true;
754 Adaptor::Get().RegisterProcessor(*this, true);
760 void TextureManager::Remove(const TextureManager::TextureId textureId)
762 if(textureId != INVALID_TEXTURE_ID)
764 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
765 if(textureCacheIndex != INVALID_CACHE_INDEX)
767 TextureManager::TextureId maskTextureId = INVALID_TEXTURE_ID;
768 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
769 // We only need to consider maskTextureId when texture's loadState is not cancelled. Because it is already deleted.
770 if(textureInfo.loadState != TextureManager::LoadState::CANCELLED && textureInfo.loadState != TextureManager::LoadState::MASK_CANCELLED)
772 if(textureInfo.maskTextureId != INVALID_TEXTURE_ID)
774 maskTextureId = textureInfo.maskTextureId;
778 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::Remove( textureId=%d ) cacheIndex:%d removal maskTextureId=%d, loadState=%s\n", textureId, textureCacheIndex.GetIndex(), maskTextureId, GET_LOAD_STATE_STRING(textureInfo.loadState));
780 // Remove textureId in CacheManager. Now, textureInfo is invalidate.
781 mTextureCacheManager.RemoveCache(textureInfo);
783 // Remove maskTextureId in CacheManager
784 if(maskTextureId != INVALID_TEXTURE_ID)
786 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
787 if(maskCacheIndex != INVALID_CACHE_INDEX)
789 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
791 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::Remove mask texture( maskTextureId=%d ) cacheIndex:%d, loadState=%s\n", maskTextureId, maskCacheIndex.GetIndex(), GET_LOAD_STATE_STRING(maskTextureInfo.loadState));
793 mTextureCacheManager.RemoveCache(maskTextureInfo);
800 void TextureManager::ProcessRemoveQueue()
802 DALI_TRACE_BEGIN_WITH_MESSAGE_GENERATOR(gTraceFilter, "DALI_TEXTURE_MANAGER_PROCESS_REMOVE_QUEUE", [&](std::ostringstream& oss) {
803 oss << "[" << mRemoveQueue.Count() << "]";
806 // Note that RemoveQueue is not be changed during Remove().
807 for(auto&& textureId : mRemoveQueue)
809 if(textureId != INVALID_TEXTURE_ID)
815 mRemoveQueue.Clear();
817 DALI_TRACE_END(gTraceFilter, "DALI_TEXTURE_MANAGER_PROCESS_REMOVE_QUEUE");
820 void TextureManager::Process(bool postProcessor)
822 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::Process()\n");
824 ProcessRemoveQueue();
826 if(Adaptor::IsAvailable())
828 Adaptor::Get().UnregisterProcessor(*this, true);
829 mRemoveProcessorRegistered = false;
833 void TextureManager::LoadImageSynchronously(
834 const VisualUrl& url,
835 const Dali::ImageDimensions& desiredSize,
836 const Dali::FittingMode::Type fittingMode,
837 const Dali::SamplingMode::Type samplingMode,
838 const bool orientationCorrection,
839 const bool loadYuvPlanes,
840 std::vector<Devel::PixelBuffer>& pixelBuffers)
842 Devel::PixelBuffer pixelBuffer;
843 if(url.IsBufferResource())
845 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
846 if(encodedImageBuffer)
848 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
855 Dali::LoadImagePlanesFromFile(url.GetUrl(), pixelBuffers, desiredSize, fittingMode, samplingMode, orientationCorrection);
859 pixelBuffer = Dali::LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
865 pixelBuffers.push_back(pixelBuffer);
869 void TextureManager::LoadOrQueueTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
871 switch(textureInfo.loadState)
873 case TextureManager::LoadState::NOT_STARTED:
874 case TextureManager::LoadState::LOAD_FAILED:
876 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
878 QueueLoadTexture(textureInfo, observer);
882 LoadTexture(textureInfo, observer);
886 case TextureManager::LoadState::UPLOADED:
888 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
890 QueueLoadTexture(textureInfo, observer);
894 // The Texture has already loaded. The other observers have already been notified.
895 // We need to send a "late" loaded notification for this observer.
898 EmitLoadComplete(observer, textureInfo, true);
903 case TextureManager::LoadState::LOADING:
904 case TextureManager::LoadState::CANCELLED:
905 case TextureManager::LoadState::MASK_CANCELLED:
906 case TextureManager::LoadState::LOAD_FINISHED:
907 case TextureManager::LoadState::WAITING_FOR_MASK:
908 case TextureManager::LoadState::MASK_APPLYING:
909 case TextureManager::LoadState::MASK_APPLIED:
916 void TextureManager::QueueLoadTexture(const TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
918 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, "Add observer to observer queue (textureId:%d, observer:%p)\n", textureInfo.textureId, observer);
920 const auto& textureId = textureInfo.textureId;
921 mLoadQueue.PushBack(QueueElement(textureId, observer));
925 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, " Connect DestructionSignal to observer:%p\n", observer);
926 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
930 void TextureManager::LoadTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
932 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::LoadTexture(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
933 textureInfo.loadState = TextureManager::LoadState::LOADING;
934 if(!textureInfo.loadSynchronously)
936 auto premultiplyOnLoad = (textureInfo.preMultiplyOnLoad && textureInfo.maskTextureId == INVALID_TEXTURE_ID) ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
937 if(textureInfo.animatedImageLoading)
939 mAsyncLoader->LoadAnimatedImage(textureInfo.textureId, textureInfo.animatedImageLoading, textureInfo.frameIndex, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, premultiplyOnLoad);
943 mAsyncLoader->Load(textureInfo.textureId, textureInfo.url, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, textureInfo.orientationCorrection, premultiplyOnLoad, textureInfo.loadYuvPlanes);
946 ObserveTexture(textureInfo, observer);
949 void TextureManager::ProcessLoadQueue()
951 for(auto&& element : mLoadQueue)
953 if(element.mTextureId == INVALID_TEXTURE_ID)
958 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(element.mTextureId);
959 if(cacheIndex != INVALID_CACHE_INDEX)
961 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
963 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::ProcessLoadQueue() textureId=%d, observer=%p, cacheIndex=@%d, loadState:%s\n", element.mTextureId, element.mObserver, cacheIndex.GetIndex(), GET_LOAD_STATE_STRING(textureInfo.loadState));
965 if((textureInfo.loadState == TextureManager::LoadState::UPLOADED) ||
966 (textureInfo.loadState == TextureManager::LoadState::LOAD_FINISHED &&
967 textureInfo.storageType == TextureManager::StorageType::RETURN_PIXEL_BUFFER))
969 if(element.mObserver)
971 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, " Disconnect DestructionSignal to observer:%p\n", element.mObserver);
972 element.mObserver->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
974 EmitLoadComplete(element.mObserver, textureInfo, true);
977 else if(textureInfo.loadState == TextureManager::LoadState::LOADING)
979 // Note : LOADING state texture cannot be queue.
980 // This case be occured when same texture id are queue in mLoadQueue.
981 ObserveTexture(textureInfo, element.mObserver);
985 LoadTexture(textureInfo, element.mObserver);
993 void TextureManager::ObserveTexture(TextureManager::TextureInfo& textureInfo,
994 TextureUploadObserver* observer)
996 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ObserveTexture(): url:%s observer:%p\n", textureInfo.url.GetUrl().c_str(), observer);
1000 textureInfo.observerList.PushBack(observer);
1002 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, " Connect DestructionSignal to observer:%p\n", observer);
1003 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
1007 void TextureManager::AsyncLoadComplete(const TextureManager::TextureId textureId, std::vector<Devel::PixelBuffer>& pixelBuffers)
1009 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1010 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::AsyncLoadComplete( textureId:%d CacheIndex:%d )\n", textureId, cacheIndex.GetIndex());
1011 if(cacheIndex != INVALID_CACHE_INDEX)
1013 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1015 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));
1016 if(textureInfo.loadState != TextureManager::LoadState::CANCELLED && textureInfo.loadState != TextureManager::LoadState::MASK_CANCELLED)
1018 // textureInfo can be invalidated after this call (as the mTextureInfoContainer may be modified)
1019 PostLoad(textureInfo, pixelBuffers);
1023 Remove(textureInfo.textureId);
1028 void TextureManager::PostLoad(TextureManager::TextureInfo& textureInfo, std::vector<Devel::PixelBuffer>& pixelBuffers)
1030 if(!pixelBuffers.empty()) ///< Load success
1032 if(pixelBuffers.size() == 1)
1034 Devel::PixelBuffer pixelBuffer = pixelBuffers[0];
1035 if(pixelBuffer && (pixelBuffer.GetWidth() != 0) && (pixelBuffer.GetHeight() != 0))
1037 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
1039 if(textureInfo.storageType == TextureManager::StorageType::UPLOAD_TO_TEXTURE)
1041 // If there is a mask texture ID associated with this texture, then apply the mask
1042 // if it's already loaded. If it hasn't, and the mask is still loading,
1043 // wait for the mask to finish loading.
1044 // note, If the texture is already uploaded synchronously during loading,
1045 // we don't need to apply mask.
1046 if(textureInfo.loadState != TextureManager::LoadState::UPLOADED &&
1047 textureInfo.maskTextureId != INVALID_TEXTURE_ID)
1049 if(textureInfo.loadState == TextureManager::LoadState::MASK_APPLYING)
1051 textureInfo.loadState = TextureManager::LoadState::MASK_APPLIED;
1052 UploadTextures(pixelBuffers, textureInfo);
1053 NotifyObservers(textureInfo, true);
1057 LoadState maskLoadState = mTextureCacheManager.GetTextureStateInternal(textureInfo.maskTextureId);
1058 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
1059 if(maskLoadState == TextureManager::LoadState::LOADING)
1061 textureInfo.loadState = TextureManager::LoadState::WAITING_FOR_MASK;
1063 else if(maskLoadState == TextureManager::LoadState::LOAD_FINISHED || maskLoadState == TextureManager::LoadState::UPLOADED)
1065 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureInfo.maskTextureId);
1066 if(maskCacheIndex != INVALID_CACHE_INDEX)
1068 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
1069 if(maskTextureInfo.storageType == TextureManager::StorageType::KEEP_PIXEL_BUFFER)
1071 ApplyMask(textureInfo, textureInfo.maskTextureId);
1073 else if(maskTextureInfo.storageType == TextureManager::StorageType::KEEP_TEXTURE)
1075 // Upload image texture. textureInfo.loadState will be UPLOADED.
1076 UploadTextures(pixelBuffers, textureInfo);
1078 // notify mask texture set.
1079 NotifyObservers(textureInfo, true);
1083 else // maskLoadState == TextureManager::LoadState::LOAD_FAILED
1085 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1086 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1087 UploadTextures(pixelBuffers, textureInfo);
1088 NotifyObservers(textureInfo, true);
1094 UploadTextures(pixelBuffers, textureInfo);
1095 NotifyObservers(textureInfo, true);
1100 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
1101 textureInfo.loadState = TextureManager::LoadState::LOAD_FINISHED;
1103 if(textureInfo.storageType == TextureManager::StorageType::RETURN_PIXEL_BUFFER)
1105 NotifyObservers(textureInfo, true);
1107 else // for the TextureManager::StorageType::KEEP_PIXEL_BUFFER and TextureManager::StorageType::KEEP_TEXTURE
1109 // Check if there was another texture waiting for this load to complete
1110 // (e.g. if this was an image mask, and its load is on a different thread)
1111 CheckForWaitingTexture(textureInfo);
1119 textureInfo.preMultiplied = false;
1121 UploadTextures(pixelBuffers, textureInfo);
1122 NotifyObservers(textureInfo, true);
1127 textureInfo.loadState = TextureManager::LoadState::LOAD_FAILED;
1128 if(textureInfo.storageType == TextureManager::StorageType::KEEP_PIXEL_BUFFER || textureInfo.storageType == TextureManager::StorageType::KEEP_TEXTURE)
1130 // Check if there was another texture waiting for this load to complete
1131 // (e.g. if this was an image mask, and its load is on a different thread)
1132 CheckForWaitingTexture(textureInfo);
1136 NotifyObservers(textureInfo, false);
1141 void TextureManager::CheckForWaitingTexture(TextureManager::TextureInfo& maskTextureInfo)
1143 if(maskTextureInfo.loadState == TextureManager::LoadState::LOAD_FINISHED &&
1144 maskTextureInfo.storageType == TextureManager::StorageType::KEEP_TEXTURE)
1146 // Upload mask texture. textureInfo.loadState will be UPLOADED.
1147 std::vector<Devel::PixelBuffer> pixelBuffers;
1148 pixelBuffers.push_back(maskTextureInfo.pixelBuffer);
1149 UploadTextures(pixelBuffers, maskTextureInfo);
1152 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): maskTextureId=%d, maskTextureUrl=%s\n", maskTextureInfo.textureId, maskTextureInfo.url.GetUrl().c_str());
1154 // Search the cache, checking if any texture has this texture id as a maskTextureId
1155 const size_t size = mTextureCacheManager.size();
1157 // Keep notify observer required textureIds.
1158 // Note : NotifyObservers can change mTextureCacheManager cache struct. We should check id's validation before notify.
1159 std::vector<TextureId> notifyRequiredTextureIds;
1161 // TODO : Refactorize here to not iterate whole cached image.
1162 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1164 if(mTextureCacheManager[cacheIndex].maskTextureId == maskTextureInfo.textureId &&
1165 mTextureCacheManager[cacheIndex].loadState == TextureManager::LoadState::WAITING_FOR_MASK)
1167 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1169 if(maskTextureInfo.loadState == TextureManager::LoadState::LOAD_FINISHED)
1171 if(maskTextureInfo.storageType == TextureManager::StorageType::KEEP_PIXEL_BUFFER)
1173 ApplyMask(textureInfo, maskTextureInfo.textureId);
1176 else if(maskTextureInfo.loadState == TextureManager::LoadState::UPLOADED)
1178 if(maskTextureInfo.storageType == TextureManager::StorageType::KEEP_TEXTURE)
1180 if(textureInfo.url.GetProtocolType() == VisualUrl::TEXTURE)
1182 // Get external textureSet from cacheManager.
1183 std::string location = textureInfo.url.GetLocation();
1184 if(!location.empty())
1186 TextureId id = std::stoi(location);
1187 auto externalTextureInfo = mTextureCacheManager.GetExternalTextureInfo(id);
1188 textureInfo.textures.push_back(externalTextureInfo.textureSet.GetTexture(0));
1189 textureInfo.loadState = TextureManager::LoadState::UPLOADED;
1194 // Upload image texture. textureInfo.loadState will be UPLOADED.
1195 std::vector<Devel::PixelBuffer> pixelBuffers;
1196 pixelBuffers.push_back(textureInfo.pixelBuffer);
1197 UploadTextures(pixelBuffers, textureInfo);
1200 // Increase reference counts for notify required textureId.
1201 // Now we can assume that we don't remove & re-assign this textureId
1202 // during NotifyObserver signal emit.
1203 maskTextureInfo.referenceCount++;
1204 textureInfo.referenceCount++;
1206 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): Ready to notify textureId=%d\n", textureInfo.textureId);
1208 notifyRequiredTextureIds.push_back(textureInfo.textureId);
1211 else // maskTextureInfo.loadState == TextureManager::LoadState::LOAD_FAILED
1213 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1214 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1215 std::vector<Devel::PixelBuffer> pixelBuffers;
1216 pixelBuffers.push_back(textureInfo.pixelBuffer);
1217 UploadTextures(pixelBuffers, textureInfo);
1219 // Increase reference counts for notify required textureId.
1220 // Now we can assume that we don't remove & re-assign this textureId
1221 // during NotifyObserver signal emit.
1222 maskTextureInfo.referenceCount++;
1223 textureInfo.referenceCount++;
1225 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): Ready to notify textureId=%d\n", textureInfo.textureId);
1227 notifyRequiredTextureIds.push_back(textureInfo.textureId);
1232 // Notify textures are masked
1233 for(const auto textureId : notifyRequiredTextureIds)
1235 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1236 if(textureCacheIndex != INVALID_CACHE_INDEX)
1238 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
1239 NotifyObservers(textureInfo, true);
1243 // Decrease reference count
1244 for(const auto textureId : notifyRequiredTextureIds)
1246 RequestRemove(textureId, nullptr);
1250 void TextureManager::ApplyMask(TextureManager::TextureInfo& textureInfo, const TextureManager::TextureId maskTextureId)
1252 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
1253 if(maskCacheIndex != INVALID_CACHE_INDEX)
1255 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
1256 Devel::PixelBuffer pixelBuffer = textureInfo.pixelBuffer;
1257 textureInfo.pixelBuffer.Reset();
1259 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ApplyMask(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
1261 textureInfo.loadState = TextureManager::LoadState::MASK_APPLYING;
1262 auto premultiplyOnLoad = textureInfo.preMultiplyOnLoad ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
1263 mAsyncLoader->ApplyMask(textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask, premultiplyOnLoad);
1267 void TextureManager::UploadTextures(std::vector<Devel::PixelBuffer>& pixelBuffers, TextureManager::TextureInfo& textureInfo)
1269 if(!pixelBuffers.empty() && textureInfo.loadState != TextureManager::LoadState::UPLOADED)
1271 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, " TextureManager::UploadTextures() New Texture for textureId:%d\n", textureInfo.textureId);
1273 // Check if this pixelBuffer is premultiplied
1274 textureInfo.preMultiplied = pixelBuffers[0].IsAlphaPreMultiplied();
1276 auto& renderingAddOn = RenderingAddOn::Get();
1277 if(renderingAddOn.IsValid())
1279 renderingAddOn.CreateGeometry(textureInfo.textureId, pixelBuffers[0]);
1282 // Remove previous textures and insert new textures
1283 textureInfo.textures.clear();
1285 for(auto&& pixelBuffer : pixelBuffers)
1287 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight());
1288 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
1289 texture.Upload(pixelData);
1290 textureInfo.textures.push_back(texture);
1294 // Update the load state.
1295 // Note: This is regardless of success as we care about whether a
1296 // load attempt is in progress or not. If unsuccessful, a broken
1297 // image is still loaded.
1298 textureInfo.loadState = TextureManager::LoadState::UPLOADED;
1301 void TextureManager::NotifyObservers(TextureManager::TextureInfo& textureInfo, const bool success)
1303 TextureId textureId = textureInfo.textureId;
1305 // If there is an observer: Notify the load is complete, whether successful or not,
1306 // and erase it from the list
1307 TextureInfo* info = &textureInfo;
1309 if(info->animatedImageLoading)
1311 // If loading failed, we don't need to get frameCount and frameInterval.
1314 info->frameCount = info->animatedImageLoading.GetImageCount();
1315 info->frameInterval = info->animatedImageLoading.GetFrameInterval(info->frameIndex);
1317 info->animatedImageLoading.Reset();
1320 mLoadingQueueTextureId = textureId;
1322 // Reverse observer list that we can pop_back the observer.
1323 std::reverse(info->observerList.Begin(), info->observerList.End());
1325 while(info->observerList.Count())
1327 TextureUploadObserver* observer = *(info->observerList.End() - 1u);
1329 // During LoadComplete() a Control ResourceReady() signal is emitted.
1330 // During that signal the app may add remove /add Textures (e.g. via
1332 // It is possible for observers to be removed from the observer list,
1333 // and it is also possible for the mTextureInfoContainer to be modified,
1334 // invalidating the reference to the textureInfo struct.
1335 // Texture load requests for the same URL are deferred until the end of this
1337 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::NotifyObservers() observer:%p textureId:%d url:%s loadState:%s\n", observer, textureId, info->url.GetUrl().c_str(), GET_LOAD_STATE_STRING(info->loadState));
1338 // It is possible for the observer to be deleted.
1339 // Disconnect and remove the observer first.
1340 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, " Disconnect DestructionSignal to observer:%p\n", observer);
1341 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1343 info->observerList.Erase(info->observerList.End() - 1u);
1345 EmitLoadComplete(observer, *info, success);
1347 // Get the textureInfo from the container again as it may have been invalidated.
1348 TextureCacheIndex textureInfoIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1349 if(textureInfoIndex == INVALID_CACHE_INDEX)
1351 break; // texture has been removed - can stop.
1353 info = &mTextureCacheManager[textureInfoIndex];
1356 mLoadingQueueTextureId = INVALID_TEXTURE_ID;
1359 if(info->storageType == TextureManager::StorageType::RETURN_PIXEL_BUFFER && info->observerList.Count() == 0)
1361 RequestRemove(info->textureId, nullptr);
1365 void TextureManager::ObserverDestroyed(TextureUploadObserver* observer)
1367 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, "TextureManager::ObserverDestroyed() observer:%p\n", observer);
1369 const std::size_t size = mTextureCacheManager.size();
1370 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1372 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1373 for(TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin();
1374 j != textureInfo.observerList.End();)
1378 j = textureInfo.observerList.Erase(j);
1387 // Remove element from the LoadQueue
1388 for(auto&& element : mLoadQueue)
1390 if(element.mObserver == observer)
1392 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, "Remove observer from observer queue (textureId:%d, observer:%p)\n", element.mTextureId, element.mObserver);
1393 element.mTextureId = INVALID_TEXTURE_ID;
1394 element.mObserver = nullptr;
1399 Dali::Geometry TextureManager::GetRenderGeometry(const TextureManager::TextureId textureId, uint32_t& frontElements, uint32_t& backElements)
1401 return RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().GetGeometry(textureId, frontElements, backElements) : Geometry();
1404 void TextureManager::EmitLoadComplete(TextureUploadObserver* observer, TextureManager::TextureInfo& textureInfo, const bool success)
1406 if(textureInfo.storageType == TextureManager::StorageType::RETURN_PIXEL_BUFFER)
1408 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::PIXEL_BUFFER, textureInfo.pixelBuffer, textureInfo.url.GetUrl(), textureInfo.preMultiplied));
1412 TextureSet textureSet = GetTextureSet(textureInfo);
1413 if(textureInfo.isAnimatedImageFormat)
1415 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::ANIMATED_IMAGE_TEXTURE, textureInfo.textureId, textureSet, textureInfo.frameCount, textureInfo.frameInterval, textureInfo.preMultiplied));
1419 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::TEXTURE, textureInfo.textureId, textureSet, textureInfo.preMultiplied));
1424 TextureSet TextureManager::GetTextureSet(const TextureManager::TextureId textureId)
1426 TextureSet textureSet;
1427 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
1428 if(loadState == TextureManager::LoadState::UPLOADED)
1430 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1431 if(textureCacheIndex != INVALID_CACHE_INDEX)
1433 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
1434 textureSet = GetTextureSet(textureInfo);
1439 DALI_LOG_ERROR("GetTextureSet is failed. texture is not uploaded \n");
1444 TextureSet TextureManager::GetTextureSet(const TextureManager::TextureInfo& textureInfo)
1446 TextureSet textureSet;
1448 // Always create new TextureSet here, so we don't share same TextureSets for multiple visuals.
1449 textureSet = TextureSet::New();
1451 if(!textureInfo.textures.empty())
1453 if(textureInfo.textures.size() > 1) // For YUV case
1455 uint32_t index = 0u;
1456 for(auto&& texture : textureInfo.textures)
1458 textureSet.SetTexture(index++, texture);
1463 textureSet.SetTexture(TEXTURE_INDEX, textureInfo.textures[0]);
1464 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureInfo.maskTextureId);
1465 if(maskCacheIndex != INVALID_CACHE_INDEX)
1467 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
1468 if(maskTextureInfo.storageType == TextureManager::StorageType::UPLOAD_TO_TEXTURE || maskTextureInfo.storageType == TextureManager::StorageType::KEEP_TEXTURE)
1470 if(!maskTextureInfo.textures.empty())
1472 textureSet.SetTexture(MASK_TEXTURE_INDEX, maskTextureInfo.textures[0]);
1481 void TextureManager::RemoveTextureObserver(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
1485 const auto iterEnd = textureInfo.observerList.End();
1486 const auto iter = std::find(textureInfo.observerList.Begin(), iterEnd, observer);
1489 // Disconnect and remove the observer.
1490 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, " Disconnect DestructionSignal to observer:%p\n", observer);
1491 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1492 textureInfo.observerList.Erase(iter);
1496 // Given textureId might exist at load queue.
1497 // Remove observer from the LoadQueue
1498 for(auto&& element : mLoadQueue)
1500 if(element.mTextureId == textureInfo.textureId && element.mObserver == observer)
1502 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, "Remove observer from observer queue (textureId:%d, observer:%p)\n", element.mTextureId, element.mObserver);
1503 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Verbose, " Disconnect DestructionSignal to observer:%p\n", observer);
1504 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1505 element.mObserver = nullptr;
1513 } // namespace Internal
1515 } // namespace Toolkit