2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/texture-manager/texture-manager-impl.h>
22 #include <dali/devel-api/adaptor-framework/environment-variable.h>
23 #include <dali/devel-api/adaptor-framework/image-loading.h>
24 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
25 #include <dali/integration-api/adaptor-framework/adaptor.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/public-api/rendering/geometry.h>
30 #include <dali-toolkit/internal/texture-manager/texture-async-loading-helper.h>
31 #include <dali-toolkit/internal/texture-manager/texture-cache-manager.h>
32 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
33 #include <dali-toolkit/internal/visuals/rendering-addon.h>
37 constexpr auto INITIAL_HASH_NUMBER = size_t{0u};
39 constexpr auto TEXTURE_INDEX = 0u; ///< The Index for texture
40 constexpr auto MASK_TEXTURE_INDEX = 1u; ///< The Index for mask texture
42 constexpr auto NUMBER_OF_LOCAL_LOADER_THREADS_ENV = "DALI_TEXTURE_LOCAL_THREADS";
43 constexpr auto NUMBER_OF_REMOTE_LOADER_THREADS_ENV = "DALI_TEXTURE_REMOTE_THREADS";
44 constexpr auto LOAD_IMAGE_YUV_PLANES_ENV = "DALI_LOAD_IMAGE_YUV_PLANES";
46 bool NeedToLoadYuvPlanes()
48 auto loadYuvPlanesString = Dali::EnvironmentVariable::GetEnvironmentVariable(LOAD_IMAGE_YUV_PLANES_ENV);
49 bool loadYuvPlanes = loadYuvPlanesString ? std::atoi(loadYuvPlanesString) : false;
61 // Define logfilter Internal namespace level so other files (e.g. texture-cache-manager.cpp) can also use this filter.
62 Debug::Filter* gTextureManagerLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TEXTURE_MANAGER");
65 #define GET_LOAD_STATE_STRING(loadState) \
66 loadState == TextureManagerType::LoadState::NOT_STARTED ? "NOT_STARTED" : \
67 loadState == TextureManagerType::LoadState::LOADING ? "LOADING" : \
68 loadState == TextureManagerType::LoadState::LOAD_FINISHED ? "LOAD_FINISHED" : \
69 loadState == TextureManagerType::LoadState::WAITING_FOR_MASK ? "WAITING_FOR_MASK" : \
70 loadState == TextureManagerType::LoadState::MASK_APPLYING ? "MASK_APPLYING" : \
71 loadState == TextureManagerType::LoadState::MASK_APPLIED ? "MASK_APPLIED" : \
72 loadState == TextureManagerType::LoadState::UPLOADED ? "UPLOADED" : \
73 loadState == TextureManagerType::LoadState::CANCELLED ? "CANCELLED" : \
74 loadState == TextureManagerType::LoadState::MASK_CANCELLED ? "MASK_CANCELLED" : \
75 loadState == TextureManagerType::LoadState::LOAD_FAILED ? "LOAD_FAILED" : \
82 const uint32_t DEFAULT_ATLAS_SIZE(1024u); ///< This size can fit 8 by 8 images of average size 128 * 128
83 const Vector4 FULL_ATLAS_RECT(0.0f, 0.0f, 1.0f, 1.0f); ///< UV Rectangle that covers the full Texture
85 void PreMultiply(Devel::PixelBuffer pixelBuffer, TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
87 if(Pixel::HasAlpha(pixelBuffer.GetPixelFormat()))
89 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
91 pixelBuffer.MultiplyColorByAlpha();
96 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
100 } // Anonymous namespace
102 TextureManager::MaskingData::MaskingData()
104 mAlphaMaskId(INVALID_TEXTURE_ID),
105 mContentScaleFactor(1.0f),
107 mPreappliedMasking(true),
108 mMaskImageLoadingFailed(false)
112 TextureManager::TextureManager()
113 : mTextureCacheManager(),
114 mAsyncLoader(std::unique_ptr<TextureAsyncLoadingHelper>(new TextureAsyncLoadingHelper(*this))),
115 mLifecycleObservers(),
117 mLoadingQueueTextureId(INVALID_TEXTURE_ID),
119 mLoadYuvPlanes(NeedToLoadYuvPlanes()),
120 mRemoveProcessorRegistered(false)
122 // Initialize the AddOn
123 RenderingAddOn::Get();
126 TextureManager::~TextureManager()
128 if(mRemoveProcessorRegistered && Adaptor::IsAvailable())
130 Adaptor::Get().UnregisterProcessor(*this, true);
131 mRemoveProcessorRegistered = false;
134 for(auto iter = mLifecycleObservers.Begin(), endIter = mLifecycleObservers.End(); iter != endIter; ++iter)
136 (*iter)->TextureManagerDestroyed();
140 TextureSet TextureManager::LoadAnimatedImageTexture(
141 const VisualUrl& url,
142 Dali::AnimatedImageLoading animatedImageLoading,
143 const uint32_t& frameIndex,
144 TextureManager::TextureId& textureId,
145 MaskingDataPointer& maskInfo,
146 const Dali::ImageDimensions& desiredSize,
147 const Dali::FittingMode::Type& fittingMode,
148 const Dali::SamplingMode::Type& samplingMode,
149 const bool& synchronousLoading,
150 TextureUploadObserver* textureObserver,
151 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
153 TextureSet textureSet;
155 if(synchronousLoading)
157 Devel::PixelBuffer pixelBuffer;
158 if(animatedImageLoading)
160 pixelBuffer = animatedImageLoading.LoadFrame(frameIndex, desiredSize, fittingMode, samplingMode);
164 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous loading is failed\n");
169 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
171 Devel::PixelBuffer maskPixelBuffer = LoadImageFromFile(maskInfo->mAlphaMaskUrl.GetUrl(), desiredSize, fittingMode, samplingMode, true);
174 if(!maskInfo->mPreappliedMasking)
176 PixelData maskPixelData = Devel::PixelBuffer::Convert(maskPixelBuffer); // takes ownership of buffer
177 maskTexture = Texture::New(Dali::TextureType::TEXTURE_2D, maskPixelData.GetPixelFormat(), maskPixelData.GetWidth(), maskPixelData.GetHeight());
178 maskTexture.Upload(maskPixelData);
182 pixelBuffer.ApplyMask(maskPixelBuffer, maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
187 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous mask image loading is failed\n");
191 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
193 PreMultiply(pixelBuffer, preMultiplyOnLoad);
196 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
199 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight());
200 texture.Upload(pixelData);
201 textureSet = TextureSet::New();
202 textureSet.SetTexture(TEXTURE_INDEX, texture);
205 textureSet.SetTexture(MASK_TEXTURE_INDEX, maskTexture);
212 TextureId alphaMaskId = INVALID_TEXTURE_ID;
213 float contentScaleFactor = 1.0f;
214 bool cropToMask = false;
215 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
217 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, maskInfo->mPreappliedMasking ? StorageType::KEEP_PIXEL_BUFFER : StorageType::KEEP_TEXTURE);
218 alphaMaskId = maskInfo->mAlphaMaskId;
219 if(maskInfo->mPreappliedMasking)
221 contentScaleFactor = maskInfo->mContentScaleFactor;
222 cropToMask = maskInfo->mCropToMask;
226 textureId = RequestLoadInternal(url, alphaMaskId, contentScaleFactor, desiredSize, fittingMode, samplingMode, UseAtlas::NO_ATLAS, cropToMask, StorageType::UPLOAD_TO_TEXTURE, textureObserver, true, TextureManager::ReloadPolicy::CACHED, preMultiplyOnLoad, animatedImageLoading, frameIndex, false);
228 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
229 if(loadState == TextureManager::LoadState::UPLOADED)
231 // LoadComplete has already been called - keep the same texture set
232 textureSet = GetTextureSet(textureId);
239 Devel::PixelBuffer TextureManager::LoadPixelBuffer(
240 const VisualUrl& url,
241 const Dali::ImageDimensions& desiredSize,
242 const Dali::FittingMode::Type& fittingMode,
243 const Dali::SamplingMode::Type& samplingMode,
244 const bool& synchronousLoading,
245 TextureUploadObserver* textureObserver,
246 const bool& orientationCorrection,
247 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
249 Devel::PixelBuffer pixelBuffer;
250 if(synchronousLoading)
254 if(url.IsBufferResource())
256 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
257 if(encodedImageBuffer)
259 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
264 pixelBuffer = LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
266 if(pixelBuffer && preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
268 PreMultiply(pixelBuffer, preMultiplyOnLoad);
274 RequestLoadInternal(url, INVALID_TEXTURE_ID, 1.0f, desiredSize, fittingMode, samplingMode, UseAtlas::NO_ATLAS, false, StorageType::RETURN_PIXEL_BUFFER, textureObserver, orientationCorrection, TextureManager::ReloadPolicy::FORCED, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, false);
280 TextureSet TextureManager::LoadTexture(
281 const VisualUrl& url,
282 const Dali::ImageDimensions& desiredSize,
283 const Dali::FittingMode::Type& fittingMode,
284 const Dali::SamplingMode::Type& samplingMode,
285 MaskingDataPointer& maskInfo,
286 const bool& synchronousLoading,
287 TextureManager::TextureId& textureId,
288 Vector4& textureRect,
289 Dali::ImageDimensions& textureRectSize,
290 bool& atlasingStatus,
292 TextureUploadObserver* textureObserver,
293 AtlasUploadObserver* atlasObserver,
294 ImageAtlasManagerPtr imageAtlasManager,
295 const bool& orientationCorrection,
296 const TextureManager::ReloadPolicy& reloadPolicy,
297 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
299 TextureSet textureSet;
301 loadingStatus = false;
302 textureRect = FULL_ATLAS_RECT;
304 if(VisualUrl::TEXTURE == url.GetProtocolType())
306 std::string location = url.GetLocation();
307 if(location.size() > 0u)
309 TextureId id = std::stoi(location);
310 auto externalTextureInfo = mTextureCacheManager.GetExternalTextureInfo(id);
311 if(externalTextureInfo.textureSet)
315 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
317 // Change preMultiplyOnLoad value so make caller determine to preMultiplyAlpha or not.
318 // TODO : Should we seperate input and output value?
319 preMultiplyOnLoad = externalTextureInfo.preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
322 TextureId alphaMaskId = INVALID_TEXTURE_ID;
323 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
325 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, StorageType::KEEP_TEXTURE, synchronousLoading);
326 alphaMaskId = maskInfo->mAlphaMaskId;
327 textureId = RequestLoad(url, alphaMaskId, 1.0f, desiredSize, fittingMode, samplingMode, UseAtlas::NO_ATLAS, false, textureObserver, orientationCorrection, reloadPolicy, preMultiplyOnLoad, synchronousLoading);
329 if(synchronousLoading)
331 auto textureSet = GetTextureSet(textureId);
332 textureSet.SetTexture(MASK_TEXTURE_INDEX, GetTextureSet(alphaMaskId).GetTexture(TEXTURE_INDEX));
338 return externalTextureInfo.textureSet;
346 if(synchronousLoading && atlasingStatus)
348 const bool synchronousAtlasAvaliable = (desiredSize != ImageDimensions() || url.IsLocalResource()) ? imageAtlasManager->CheckAtlasAvailable(url, desiredSize)
350 if(synchronousAtlasAvaliable)
352 std::vector<Devel::PixelBuffer> pixelBuffers;
353 LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection, false, pixelBuffers);
355 if(!pixelBuffers.empty() && maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
357 std::vector<Devel::PixelBuffer> maskPixelBuffers;
358 LoadImageSynchronously(maskInfo->mAlphaMaskUrl, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true, false, maskPixelBuffers);
359 if(!maskPixelBuffers.empty())
361 pixelBuffers[0].ApplyMask(maskPixelBuffers[0], maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
366 if(!pixelBuffers.empty())
368 PreMultiply(pixelBuffers[0], preMultiplyOnLoad);
369 data = Devel::PixelBuffer::Convert(pixelBuffers[0]); // takes ownership of buffer
373 textureSet = imageAtlasManager->Add(textureRect, data);
376 textureRectSize.SetWidth(data.GetWidth());
377 textureRectSize.SetHeight(data.GetHeight());
382 DALI_LOG_ERROR("TextureManager::LoadTexture: Synchronous Texture loading with atlasing is failed.\n");
387 atlasingStatus = false;
394 loadingStatus = true;
395 // Atlas manager can chage desired size when it is set by 0,0.
396 // We should store into textureRectSize only if atlasing successed.
397 // So copy inputed desiredSize, and replace value into textureRectSize only if atlasing success.
398 Dali::ImageDimensions atlasDesiredSize = desiredSize;
401 if(url.IsBufferResource())
403 const EncodedImageBuffer& encodedImageBuffer = GetEncodedImageBuffer(url.GetUrl());
404 if(encodedImageBuffer)
406 textureSet = imageAtlasManager->Add(textureRect, encodedImageBuffer, desiredSize, fittingMode, true, atlasObserver);
411 textureSet = imageAtlasManager->Add(textureRect, url, atlasDesiredSize, fittingMode, true, atlasObserver);
414 if(!textureSet) // big image, no atlasing or atlasing failed
416 atlasingStatus = false;
418 TextureId alphaMaskId = INVALID_TEXTURE_ID;
419 float contentScaleFactor = 1.0f;
420 bool cropToMask = false;
421 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
423 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, maskInfo->mPreappliedMasking ? StorageType::KEEP_PIXEL_BUFFER : StorageType::KEEP_TEXTURE, synchronousLoading);
424 alphaMaskId = maskInfo->mAlphaMaskId;
425 if(maskInfo && maskInfo->mPreappliedMasking)
427 contentScaleFactor = maskInfo->mContentScaleFactor;
428 cropToMask = maskInfo->mCropToMask;
432 textureId = RequestLoad(
442 orientationCorrection,
447 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
448 if(loadState == TextureManager::LoadState::UPLOADED)
450 // LoadComplete has already been called - keep the same texture set
451 textureSet = GetTextureSet(textureId);
454 // If we are loading the texture, or waiting for the ready signal handler to complete, inform
455 // caller that they need to wait.
456 loadingStatus = (loadState == TextureManager::LoadState::LOADING ||
457 loadState == TextureManager::LoadState::WAITING_FOR_MASK ||
458 loadState == TextureManager::LoadState::MASK_APPLYING ||
459 loadState == TextureManager::LoadState::MASK_APPLIED ||
460 loadState == TextureManager::LoadState::NOT_STARTED ||
461 mLoadingQueueTextureId != INVALID_TEXTURE_ID);
465 textureRectSize = atlasDesiredSize;
470 if(synchronousLoading)
472 loadingStatus = false;
478 TextureManager::TextureId TextureManager::RequestLoad(
479 const VisualUrl& url,
480 const ImageDimensions& desiredSize,
481 const Dali::FittingMode::Type& fittingMode,
482 const Dali::SamplingMode::Type& samplingMode,
483 const UseAtlas& useAtlas,
484 TextureUploadObserver* observer,
485 const bool& orientationCorrection,
486 const TextureManager::ReloadPolicy& reloadPolicy,
487 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
488 const bool& synchronousLoading)
490 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);
493 TextureManager::TextureId TextureManager::RequestLoad(
494 const VisualUrl& url,
495 const TextureManager::TextureId& maskTextureId,
496 const float& contentScale,
497 const Dali::ImageDimensions& desiredSize,
498 const Dali::FittingMode::Type& fittingMode,
499 const Dali::SamplingMode::Type& samplingMode,
500 const TextureManager::UseAtlas& useAtlas,
501 const bool& cropToMask,
502 TextureUploadObserver* observer,
503 const bool& orientationCorrection,
504 const TextureManager::ReloadPolicy& reloadPolicy,
505 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
506 const bool& synchronousLoading)
508 return RequestLoadInternal(url, maskTextureId, contentScale, desiredSize, fittingMode, samplingMode, useAtlas, cropToMask, StorageType::UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
511 TextureManager::TextureId TextureManager::RequestMaskLoad(
512 const VisualUrl& maskUrl,
513 StorageType storageType,
514 const bool& synchronousLoading)
516 // Use the normal load procedure to get the alpha mask.
517 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
518 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);
521 TextureManager::TextureId TextureManager::RequestLoadInternal(
522 const VisualUrl& url,
523 const TextureManager::TextureId& maskTextureId,
524 const float& contentScale,
525 const Dali::ImageDimensions& desiredSize,
526 const Dali::FittingMode::Type& fittingMode,
527 const Dali::SamplingMode::Type& samplingMode,
528 const TextureManager::UseAtlas& useAtlas,
529 const bool& cropToMask,
530 const TextureManager::StorageType& storageType,
531 TextureUploadObserver* observer,
532 const bool& orientationCorrection,
533 const TextureManager::ReloadPolicy& reloadPolicy,
534 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
535 Dali::AnimatedImageLoading animatedImageLoading,
536 const std::uint32_t& frameIndex,
537 const bool& synchronousLoading)
539 TextureHash textureHash = INITIAL_HASH_NUMBER;
540 TextureCacheIndex cacheIndex = INVALID_CACHE_INDEX;
541 bool loadYuvPlanes = (mLoadYuvPlanes && maskTextureId == INVALID_TEXTURE_ID && storageType == StorageType::UPLOAD_TO_TEXTURE);
543 if(storageType != StorageType::RETURN_PIXEL_BUFFER)
545 textureHash = mTextureCacheManager.GenerateHash(url, desiredSize, fittingMode, samplingMode, useAtlas, maskTextureId, cropToMask, frameIndex);
547 // Look up the texture by hash. Note: The extra parameters are used in case of a hash collision.
548 cacheIndex = mTextureCacheManager.FindCachedTexture(textureHash, url, desiredSize, fittingMode, samplingMode, useAtlas, storageType, maskTextureId, cropToMask, preMultiplyOnLoad, (animatedImageLoading) ? true : false, frameIndex);
551 TextureManager::TextureId textureId = INVALID_TEXTURE_ID;
552 // Check if the requested Texture exists in the cache.
553 if(cacheIndex != INVALID_CACHE_INDEX)
555 if(TextureManager::ReloadPolicy::CACHED == reloadPolicy)
557 // Mark this texture being used by another client resource. Forced reload would replace the current texture
558 // without the need for incrementing the reference count.
559 ++(mTextureCacheManager[cacheIndex].referenceCount);
561 textureId = mTextureCacheManager[cacheIndex].textureId;
563 // Update preMultiplyOnLoad value. It should be changed according to preMultiplied value of the cached info.
564 preMultiplyOnLoad = mTextureCacheManager[cacheIndex].preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
565 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);
568 if(textureId == INVALID_TEXTURE_ID) // There was no caching, or caching not required
570 textureId = mTextureCacheManager.GenerateTextureId();
572 bool preMultiply = (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
574 // Cache new texutre, and get cacheIndex.
575 cacheIndex = mTextureCacheManager.AppendCache(TextureInfo(textureId, maskTextureId, url, desiredSize, contentScale, fittingMode, samplingMode, false, cropToMask, useAtlas, textureHash, orientationCorrection, preMultiply, animatedImageLoading, frameIndex, loadYuvPlanes));
576 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);
579 // The below code path is common whether we are using the cache or not.
580 // The textureInfoIndex now refers to either a pre-existing cached TextureInfo,
581 // or a new TextureInfo just created.
582 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
583 textureInfo.maskTextureId = maskTextureId;
584 textureInfo.storageType = storageType;
585 textureInfo.orientationCorrection = orientationCorrection;
587 // the case using external texture has already been loaded texture, so change its status to WAITING_FOR_MASK.
588 if(url.GetProtocolType() == VisualUrl::TEXTURE)
590 if(textureInfo.loadState != LoadState::UPLOADED)
592 textureInfo.loadState = TextureManager::LoadState::WAITING_FOR_MASK;
596 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureInfo loadState:%s\n", GET_LOAD_STATE_STRING(textureInfo.loadState));
598 // Force reloading of texture by setting loadState unless already loading or cancelled.
599 if(TextureManager::ReloadPolicy::FORCED == reloadPolicy &&
600 TextureManager::LoadState::LOADING != textureInfo.loadState &&
601 TextureManager::LoadState::WAITING_FOR_MASK != textureInfo.loadState &&
602 TextureManager::LoadState::MASK_APPLYING != textureInfo.loadState &&
603 TextureManager::LoadState::MASK_APPLIED != textureInfo.loadState &&
604 TextureManager::LoadState::CANCELLED != textureInfo.loadState &&
605 TextureManager::LoadState::MASK_CANCELLED != textureInfo.loadState)
607 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);
608 textureInfo.loadState = TextureManager::LoadState::NOT_STARTED;
611 if(!synchronousLoading)
613 // Check if we should add the observer.
614 // Only do this if we have not loaded yet and it will not have loaded by the end of this method.
615 switch(textureInfo.loadState)
617 case TextureManager::LoadState::LOAD_FAILED: // Failed notifies observer which then stops observing.
618 case TextureManager::LoadState::NOT_STARTED:
620 LoadOrQueueTexture(textureInfo, observer); // If called inside NotifyObservers, queues until afterwards
623 case TextureManager::LoadState::LOADING:
624 case TextureManager::LoadState::WAITING_FOR_MASK:
625 case TextureManager::LoadState::MASK_APPLYING:
626 case TextureManager::LoadState::MASK_APPLIED:
628 ObserveTexture(textureInfo, observer);
631 case TextureManager::LoadState::UPLOADED:
635 LoadOrQueueTexture(textureInfo, observer);
639 case TextureManager::LoadState::CANCELLED:
641 // A cancelled texture hasn't finished loading yet. Treat as a loading texture
642 // (it's ref count has already been incremented, above)
643 textureInfo.loadState = TextureManager::LoadState::LOADING;
644 ObserveTexture(textureInfo, observer);
647 case TextureManager::LoadState::MASK_CANCELLED:
649 // A cancelled texture hasn't finished mask applying yet. Treat as a mask applying texture
650 // (it's ref count has already been incremented, above)
651 textureInfo.loadState = TextureManager::LoadState::MASK_APPLYING;
652 ObserveTexture(textureInfo, observer);
655 case TextureManager::LoadState::LOAD_FINISHED:
657 // Loading has already completed.
658 if(observer && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
660 LoadOrQueueTexture(textureInfo, observer);
668 // If the image is already finished to load, use cached texture.
669 // We don't need to consider Observer because this is synchronous loading.
670 if(!(textureInfo.loadState == TextureManager::LoadState::UPLOADED ||
671 textureInfo.loadState == TextureManager::LoadState::LOAD_FINISHED))
673 if(url.GetProtocolType() == VisualUrl::TEXTURE)
675 // Get external textureSet from cacheManager.
676 std::string location = textureInfo.url.GetLocation();
677 if(!location.empty())
679 TextureId id = std::stoi(location);
680 auto externalTextureInfo = mTextureCacheManager.GetExternalTextureInfo(id);
681 textureInfo.textures.push_back(externalTextureInfo.textureSet.GetTexture(0));
682 textureInfo.loadState = LoadState::UPLOADED;
687 std::vector<Devel::PixelBuffer> pixelBuffers;
688 LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection, loadYuvPlanes, pixelBuffers);
690 if(pixelBuffers.empty())
692 // If pixelBuffer loading is failed in synchronously, call RequestRemove() method.
693 RequestRemove(textureId, nullptr);
694 return INVALID_TEXTURE_ID;
697 if(storageType == StorageType::KEEP_PIXEL_BUFFER) // For the mask image loading.
699 textureInfo.pixelBuffer = pixelBuffers[0]; // Store the pixel data
700 textureInfo.loadState = LoadState::LOAD_FINISHED;
702 else // For the image loading.
705 if(maskTextureId != INVALID_TEXTURE_ID)
707 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
708 if(maskCacheIndex != INVALID_CACHE_INDEX)
710 if(mTextureCacheManager[maskCacheIndex].storageType == StorageType::KEEP_TEXTURE)
712 if(!mTextureCacheManager[maskCacheIndex].textures.empty())
714 maskTexture = mTextureCacheManager[maskCacheIndex].textures[0];
717 else if(mTextureCacheManager[maskCacheIndex].storageType == StorageType::KEEP_PIXEL_BUFFER)
719 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
722 pixelBuffers[0].ApplyMask(maskPixelBuffer, contentScale, cropToMask);
726 DALI_LOG_ERROR("Mask image cached invalid pixel buffer!\n");
732 DALI_LOG_ERROR("Mask image is not stored in cache.\n");
735 PreMultiply(pixelBuffers[0], preMultiplyOnLoad);
738 UploadTextures(pixelBuffers, textureInfo);
747 void TextureManager::RequestRemove(const TextureManager::TextureId& textureId, TextureUploadObserver* observer)
749 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::RequestRemove( textureId=%d observer=%p )\n", textureId, observer);
752 if(textureId != INVALID_TEXTURE_ID)
756 // Remove observer from cached texture info
757 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
758 if(textureCacheIndex != INVALID_CACHE_INDEX)
760 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
761 RemoveTextureObserver(textureInfo, observer);
764 mRemoveQueue.PushBack(textureId);
766 if(!mRemoveProcessorRegistered && Adaptor::IsAvailable())
768 mRemoveProcessorRegistered = true;
769 Adaptor::Get().RegisterProcessor(*this, true);
774 void TextureManager::Remove(const TextureManager::TextureId& textureId)
776 if(textureId != INVALID_TEXTURE_ID)
778 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
779 if(textureCacheIndex != INVALID_CACHE_INDEX)
781 TextureManager::TextureId maskTextureId = INVALID_TEXTURE_ID;
782 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
783 // We only need to consider maskTextureId when texture's loadState is not cancelled. Because it is already deleted.
784 if(textureInfo.loadState != LoadState::CANCELLED && textureInfo.loadState != LoadState::MASK_CANCELLED)
786 if(textureInfo.maskTextureId != INVALID_TEXTURE_ID)
788 maskTextureId = textureInfo.maskTextureId;
792 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::Remove( textureId=%d ) cacheIndex:%d removal maskTextureId=%d, loadingQueueTextureId=%d, loadState=%s\n", textureId, textureCacheIndex.GetIndex(), maskTextureId, mLoadingQueueTextureId, GET_LOAD_STATE_STRING(textureInfo.loadState));
794 // Remove textureId in CacheManager. Now, textureInfo is invalidate.
795 mTextureCacheManager.RemoveCache(textureInfo);
797 // Remove maskTextureId in CacheManager
798 if(maskTextureId != INVALID_TEXTURE_ID)
800 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
801 if(maskCacheIndex != INVALID_CACHE_INDEX)
803 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
804 mTextureCacheManager.RemoveCache(maskTextureInfo);
811 void TextureManager::ProcessRemoveQueue()
813 // Note that RemoveQueue is not be changed during Remove().
814 for(auto&& textureId : mRemoveQueue)
816 if(textureId != INVALID_TEXTURE_ID)
821 mRemoveQueue.Clear();
824 void TextureManager::Process(bool postProcessor)
826 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::Process()\n");
828 ProcessRemoveQueue();
830 if(Adaptor::IsAvailable())
832 Adaptor::Get().UnregisterProcessor(*this, true);
833 mRemoveProcessorRegistered = false;
837 void TextureManager::LoadImageSynchronously(
838 const VisualUrl& url,
839 const Dali::ImageDimensions& desiredSize,
840 const Dali::FittingMode::Type& fittingMode,
841 const Dali::SamplingMode::Type& samplingMode,
842 const bool& orientationCorrection,
843 const bool& loadYuvPlanes,
844 std::vector<Devel::PixelBuffer>& pixelBuffers)
846 Devel::PixelBuffer pixelBuffer;
847 if(url.IsBufferResource())
849 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
850 if(encodedImageBuffer)
852 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
859 Dali::LoadImagePlanesFromFile(url.GetUrl(), pixelBuffers, desiredSize, fittingMode, samplingMode, orientationCorrection);
863 pixelBuffer = Dali::LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
869 pixelBuffers.push_back(pixelBuffer);
873 void TextureManager::AddObserver(TextureManager::LifecycleObserver& observer)
875 // make sure an observer doesn't observe the same object twice
876 // otherwise it will get multiple calls to ObjectDestroyed()
877 DALI_ASSERT_DEBUG(mLifecycleObservers.End() == std::find(mLifecycleObservers.Begin(), mLifecycleObservers.End(), &observer));
878 mLifecycleObservers.PushBack(&observer);
881 void TextureManager::RemoveObserver(TextureManager::LifecycleObserver& observer)
883 // Find the observer...
884 auto endIter = mLifecycleObservers.End();
885 for(auto iter = mLifecycleObservers.Begin(); iter != endIter; ++iter)
887 if((*iter) == &observer)
889 mLifecycleObservers.Erase(iter);
893 DALI_ASSERT_DEBUG(endIter != mLifecycleObservers.End());
896 void TextureManager::LoadOrQueueTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
898 switch(textureInfo.loadState)
900 case LoadState::NOT_STARTED:
901 case LoadState::LOAD_FAILED:
903 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
905 QueueLoadTexture(textureInfo, observer);
909 LoadTexture(textureInfo, observer);
913 case LoadState::UPLOADED:
915 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
917 QueueLoadTexture(textureInfo, observer);
921 // The Texture has already loaded. The other observers have already been notified.
922 // We need to send a "late" loaded notification for this observer.
923 EmitLoadComplete(observer, textureInfo, true);
927 case LoadState::LOADING:
928 case LoadState::CANCELLED:
929 case LoadState::MASK_CANCELLED:
930 case LoadState::LOAD_FINISHED:
931 case LoadState::WAITING_FOR_MASK:
932 case LoadState::MASK_APPLYING:
933 case LoadState::MASK_APPLIED:
940 void TextureManager::QueueLoadTexture(const TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
942 const auto& textureId = textureInfo.textureId;
943 mLoadQueue.PushBack(QueueElement(textureId, observer));
947 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
951 void TextureManager::LoadTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
953 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::LoadTexture(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
954 textureInfo.loadState = LoadState::LOADING;
955 if(!textureInfo.loadSynchronously)
957 auto premultiplyOnLoad = (textureInfo.preMultiplyOnLoad && textureInfo.maskTextureId == INVALID_TEXTURE_ID) ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
958 if(textureInfo.animatedImageLoading)
960 mAsyncLoader->LoadAnimatedImage(textureInfo.textureId, textureInfo.animatedImageLoading, textureInfo.frameIndex, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, premultiplyOnLoad);
964 mAsyncLoader->Load(textureInfo.textureId, textureInfo.url, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, textureInfo.orientationCorrection, premultiplyOnLoad, textureInfo.loadYuvPlanes);
967 ObserveTexture(textureInfo, observer);
970 void TextureManager::ProcessLoadQueue()
972 for(auto&& element : mLoadQueue)
974 if(element.mTextureId == INVALID_TEXTURE_ID)
979 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(element.mTextureId);
980 if(cacheIndex != INVALID_CACHE_INDEX)
982 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
983 if((textureInfo.loadState == LoadState::UPLOADED) || (textureInfo.loadState == LoadState::LOAD_FINISHED && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER))
985 if(element.mObserver)
987 EmitLoadComplete(element.mObserver, textureInfo, true);
990 else if(textureInfo.loadState == LoadState::LOADING)
992 // Note : LOADING state texture cannot be queue.
993 // This case be occured when same texture id are queue in mLoadQueue.
994 ObserveTexture(textureInfo, element.mObserver);
998 LoadTexture(textureInfo, element.mObserver);
1005 void TextureManager::ObserveTexture(TextureManager::TextureInfo& textureInfo,
1006 TextureUploadObserver* observer)
1008 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ObserveTexture(): url:%s observer:%p\n", textureInfo.url.GetUrl().c_str(), observer);
1012 textureInfo.observerList.PushBack(observer);
1013 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
1017 void TextureManager::AsyncLoadComplete(const TextureManager::TextureId& textureId, std::vector<Devel::PixelBuffer>& pixelBuffers)
1019 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1020 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::AsyncLoadComplete( textureId:%d CacheIndex:%d )\n", textureId, cacheIndex.GetIndex());
1021 if(cacheIndex != INVALID_CACHE_INDEX)
1023 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1025 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));
1026 if(textureInfo.loadState != LoadState::CANCELLED && textureInfo.loadState != LoadState::MASK_CANCELLED)
1028 // textureInfo can be invalidated after this call (as the mTextureInfoContainer may be modified)
1029 PostLoad(textureInfo, pixelBuffers);
1033 RequestRemove(textureInfo.textureId, nullptr);
1038 void TextureManager::PostLoad(TextureManager::TextureInfo& textureInfo, std::vector<Devel::PixelBuffer>& pixelBuffers)
1040 // Was the load successful?
1041 if(!pixelBuffers.empty())
1043 if(pixelBuffers.size() == 1)
1045 Devel::PixelBuffer pixelBuffer = pixelBuffers[0];
1046 if(pixelBuffer && (pixelBuffer.GetWidth() != 0) && (pixelBuffer.GetHeight() != 0))
1048 // No atlas support for now
1049 textureInfo.useAtlas = UseAtlas::NO_ATLAS;
1050 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
1052 if(textureInfo.storageType == StorageType::UPLOAD_TO_TEXTURE)
1054 // If there is a mask texture ID associated with this texture, then apply the mask
1055 // if it's already loaded. If it hasn't, and the mask is still loading,
1056 // wait for the mask to finish loading.
1057 // note, If the texture is already uploaded synchronously during loading,
1058 // we don't need to apply mask.
1059 if(textureInfo.loadState != LoadState::UPLOADED &&
1060 textureInfo.maskTextureId != INVALID_TEXTURE_ID)
1062 if(textureInfo.loadState == LoadState::MASK_APPLYING)
1064 textureInfo.loadState = LoadState::MASK_APPLIED;
1065 UploadTextures(pixelBuffers, textureInfo);
1066 NotifyObservers(textureInfo, true);
1070 LoadState maskLoadState = mTextureCacheManager.GetTextureStateInternal(textureInfo.maskTextureId);
1071 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
1072 if(maskLoadState == LoadState::LOADING)
1074 textureInfo.loadState = LoadState::WAITING_FOR_MASK;
1076 else if(maskLoadState == LoadState::LOAD_FINISHED || maskLoadState == LoadState::UPLOADED)
1078 // Send New Task to Thread
1079 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureInfo.maskTextureId);
1080 if(maskCacheIndex != INVALID_CACHE_INDEX)
1082 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
1083 if(maskTextureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER)
1085 // Send New Task to Thread
1086 ApplyMask(textureInfo, textureInfo.maskTextureId);
1088 else if(maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1090 // Upload image texture. textureInfo.loadState will be UPLOADED.
1091 UploadTextures(pixelBuffers, textureInfo);
1093 // notify mask texture set.
1094 NotifyObservers(textureInfo, true);
1098 else // maskLoadState == LoadState::LOAD_FAILED
1100 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1101 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1102 UploadTextures(pixelBuffers, textureInfo);
1103 NotifyObservers(textureInfo, true);
1109 UploadTextures(pixelBuffers, textureInfo);
1110 NotifyObservers(textureInfo, true);
1115 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
1116 textureInfo.loadState = LoadState::LOAD_FINISHED;
1118 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1120 NotifyObservers(textureInfo, true);
1122 else // for the StorageType::KEEP_PIXEL_BUFFER and StorageType::KEEP_TEXTURE
1124 // Check if there was another texture waiting for this load to complete
1125 // (e.g. if this was an image mask, and its load is on a different thread)
1126 CheckForWaitingTexture(textureInfo);
1134 // No atlas support for now
1135 textureInfo.useAtlas = UseAtlas::NO_ATLAS;
1136 textureInfo.preMultiplied = false;
1138 UploadTextures(pixelBuffers, textureInfo);
1139 NotifyObservers(textureInfo, true);
1144 textureInfo.loadState = LoadState::LOAD_FAILED;
1145 if(textureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER || textureInfo.storageType == StorageType::KEEP_TEXTURE)
1147 // Check if there was another texture waiting for this load to complete
1148 // (e.g. if this was an image mask, and its load is on a different thread)
1149 CheckForWaitingTexture(textureInfo);
1153 NotifyObservers(textureInfo, false);
1158 void TextureManager::CheckForWaitingTexture(TextureManager::TextureInfo& maskTextureInfo)
1160 if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED &&
1161 maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1163 // Upload mask texture. textureInfo.loadState will be UPLOADED.
1164 std::vector<Devel::PixelBuffer> pixelBuffers;
1165 pixelBuffers.push_back(maskTextureInfo.pixelBuffer);
1166 UploadTextures(pixelBuffers, maskTextureInfo);
1169 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): maskTextureId=%d, maskTextureUrl=%s\n", maskTextureInfo.textureId, maskTextureInfo.url.GetUrl().c_str());
1171 // Search the cache, checking if any texture has this texture id as a maskTextureId
1172 const std::size_t size = mTextureCacheManager.size();
1174 // Keep notify observer required textureIds.
1175 // Note : NotifyObservers can change mTextureCacheManager cache struct. We should check id's validation before notify.
1176 std::vector<TextureId> notifyRequiredTextureIds;
1178 // TODO : Refactorize here to not iterate whole cached image.
1179 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1181 if(mTextureCacheManager[cacheIndex].maskTextureId == maskTextureInfo.textureId &&
1182 mTextureCacheManager[cacheIndex].loadState == LoadState::WAITING_FOR_MASK)
1184 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1186 if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED)
1188 if(maskTextureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER)
1190 // Send New Task to Thread
1191 ApplyMask(textureInfo, maskTextureInfo.textureId);
1194 else if(maskTextureInfo.loadState == LoadState::UPLOADED)
1196 if(maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1198 if(textureInfo.url.GetProtocolType() == VisualUrl::TEXTURE)
1200 // Get external textureSet from cacheManager.
1201 std::string location = textureInfo.url.GetLocation();
1202 if(!location.empty())
1204 TextureId id = std::stoi(location);
1205 auto externalTextureInfo = mTextureCacheManager.GetExternalTextureInfo(id);
1206 textureInfo.textures.push_back(externalTextureInfo.textureSet.GetTexture(0));
1211 // Upload image texture. textureInfo.loadState will be UPLOADED.
1212 std::vector<Devel::PixelBuffer> pixelBuffers;
1213 pixelBuffers.push_back(textureInfo.pixelBuffer);
1214 UploadTextures(pixelBuffers, textureInfo);
1217 // Increase reference counts for notify required textureId.
1218 // Now we can assume that we don't remove & re-assign this textureId
1219 // during NotifyObserver signal emit.
1220 maskTextureInfo.referenceCount++;
1221 textureInfo.referenceCount++;
1223 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): Ready to notify textureId=%d\n", textureInfo.textureId);
1225 notifyRequiredTextureIds.push_back(textureInfo.textureId);
1228 else // maskTextureInfo.loadState == LoadState::LOAD_FAILED
1230 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1231 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1232 std::vector<Devel::PixelBuffer> pixelBuffers;
1233 pixelBuffers.push_back(textureInfo.pixelBuffer);
1234 UploadTextures(pixelBuffers, textureInfo);
1236 // Increase reference counts for notify required textureId.
1237 // Now we can assume that we don't remove & re-assign this textureId
1238 // during NotifyObserver signal emit.
1239 maskTextureInfo.referenceCount++;
1240 textureInfo.referenceCount++;
1242 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): Ready to notify textureId=%d\n", textureInfo.textureId);
1244 notifyRequiredTextureIds.push_back(textureInfo.textureId);
1249 // Notify textures are masked
1250 for(const auto textureId : notifyRequiredTextureIds)
1252 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1253 if(textureCacheIndex != INVALID_CACHE_INDEX)
1255 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
1256 NotifyObservers(textureInfo, true);
1260 // Decrease reference count
1261 for(const auto textureId : notifyRequiredTextureIds)
1263 RequestRemove(textureId, nullptr);
1267 void TextureManager::ApplyMask(TextureManager::TextureInfo& textureInfo, const TextureManager::TextureId& maskTextureId)
1269 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
1270 if(maskCacheIndex != INVALID_CACHE_INDEX)
1272 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
1273 Devel::PixelBuffer pixelBuffer = textureInfo.pixelBuffer;
1274 textureInfo.pixelBuffer.Reset();
1276 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ApplyMask(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
1278 textureInfo.loadState = LoadState::MASK_APPLYING;
1279 auto premultiplyOnLoad = textureInfo.preMultiplyOnLoad ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
1280 mAsyncLoader->ApplyMask(textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask, premultiplyOnLoad);
1284 void TextureManager::UploadTextures(std::vector<Devel::PixelBuffer>& pixelBuffers, TextureManager::TextureInfo& textureInfo)
1286 if(!pixelBuffers.empty() && textureInfo.loadState != LoadState::UPLOADED && textureInfo.useAtlas != UseAtlas::USE_ATLAS)
1288 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, " TextureManager::UploadTextures() New Texture for textureId:%d\n", textureInfo.textureId);
1290 // Check if this pixelBuffer is premultiplied
1291 textureInfo.preMultiplied = pixelBuffers[0].IsAlphaPreMultiplied();
1293 auto& renderingAddOn = RenderingAddOn::Get();
1294 if(renderingAddOn.IsValid())
1296 renderingAddOn.CreateGeometry(textureInfo.textureId, pixelBuffers[0]);
1299 // Remove previous textures and insert new textures
1300 textureInfo.textures.clear();
1302 for(auto&& pixelBuffer : pixelBuffers)
1304 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight());
1305 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
1306 texture.Upload(pixelData);
1307 textureInfo.textures.push_back(texture);
1311 // Update the load state.
1312 // Note: This is regardless of success as we care about whether a
1313 // load attempt is in progress or not. If unsuccessful, a broken
1314 // image is still loaded.
1315 textureInfo.loadState = LoadState::UPLOADED;
1318 void TextureManager::NotifyObservers(TextureManager::TextureInfo& textureInfo, const bool& success)
1320 TextureId textureId = textureInfo.textureId;
1322 // If there is an observer: Notify the load is complete, whether successful or not,
1323 // and erase it from the list
1324 TextureInfo* info = &textureInfo;
1326 if(info->animatedImageLoading)
1328 // If loading failed, we don't need to get frameCount and frameInterval.
1331 info->frameCount = info->animatedImageLoading.GetImageCount();
1332 info->frameInterval = info->animatedImageLoading.GetFrameInterval(info->frameIndex);
1334 info->animatedImageLoading.Reset();
1337 mLoadingQueueTextureId = textureId;
1339 // Reverse observer list that we can pop_back the observer.
1340 std::reverse(info->observerList.Begin(), info->observerList.End());
1342 while(info->observerList.Count())
1344 TextureUploadObserver* observer = *(info->observerList.End() - 1u);
1346 // During LoadComplete() a Control ResourceReady() signal is emitted.
1347 // During that signal the app may add remove /add Textures (e.g. via
1349 // It is possible for observers to be removed from the observer list,
1350 // and it is also possible for the mTextureInfoContainer to be modified,
1351 // invalidating the reference to the textureInfo struct.
1352 // Texture load requests for the same URL are deferred until the end of this
1354 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));
1355 // It is possible for the observer to be deleted.
1356 // Disconnect and remove the observer first.
1357 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1359 info->observerList.Erase(info->observerList.End() - 1u);
1361 EmitLoadComplete(observer, *info, success);
1363 // Get the textureInfo from the container again as it may have been invalidated.
1364 TextureCacheIndex textureInfoIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1365 if(textureInfoIndex == INVALID_CACHE_INDEX)
1367 break; // texture has been removed - can stop.
1369 info = &mTextureCacheManager[textureInfoIndex];
1372 mLoadingQueueTextureId = INVALID_TEXTURE_ID;
1375 if(info->storageType == StorageType::RETURN_PIXEL_BUFFER && info->observerList.Count() == 0)
1377 RequestRemove(info->textureId, nullptr);
1381 void TextureManager::ObserverDestroyed(TextureUploadObserver* observer)
1383 const std::size_t size = mTextureCacheManager.size();
1384 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1386 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1387 for(TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin();
1388 j != textureInfo.observerList.End();)
1392 j = textureInfo.observerList.Erase(j);
1401 // Remove element from the LoadQueue
1402 for(auto&& element : mLoadQueue)
1404 if(element.mObserver == observer)
1406 element.mTextureId = INVALID_TEXTURE_ID;
1407 element.mObserver = nullptr;
1412 Dali::Geometry TextureManager::GetRenderGeometry(const TextureManager::TextureId& textureId, std::uint32_t& frontElements, std::uint32_t& backElements)
1414 return RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().GetGeometry(textureId, frontElements, backElements) : Geometry();
1417 void TextureManager::EmitLoadComplete(TextureUploadObserver* observer, TextureManager::TextureInfo& textureInfo, const bool& success)
1419 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1421 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::PIXEL_BUFFER, textureInfo.pixelBuffer, textureInfo.url.GetUrl(), textureInfo.preMultiplied));
1425 TextureSet textureSet = GetTextureSet(textureInfo);
1426 if(textureInfo.isAnimatedImageFormat)
1428 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::ANIMATED_IMAGE_TEXTURE, textureInfo.textureId, textureSet, textureInfo.frameCount, textureInfo.frameInterval));
1432 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::TEXTURE, textureInfo.textureId, textureSet, (textureInfo.useAtlas == UseAtlas::USE_ATLAS) ? true : false, textureInfo.atlasRect, textureInfo.preMultiplied));
1437 TextureSet TextureManager::GetTextureSet(const TextureManager::TextureId& textureId)
1439 TextureSet textureSet;
1440 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
1441 if(loadState == TextureManager::LoadState::UPLOADED)
1443 // LoadComplete has already been called - keep the same texture set
1444 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1445 if(textureCacheIndex != INVALID_CACHE_INDEX)
1447 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
1448 textureSet = GetTextureSet(textureInfo);
1453 DALI_LOG_ERROR("GetTextureSet is failed. texture is not uploaded \n");
1458 TextureSet TextureManager::GetTextureSet(const TextureManager::TextureInfo& textureInfo)
1460 TextureSet textureSet;
1462 // LoadComplete has already been called - keep the same texture set
1463 textureSet = TextureSet::New();
1464 if(!textureInfo.textures.empty())
1466 if(textureInfo.textures.size() > 1) // For YUV case
1468 uint32_t index = 0u;
1469 for(auto&& texture : textureInfo.textures)
1471 textureSet.SetTexture(index++, texture);
1476 textureSet.SetTexture(TEXTURE_INDEX, textureInfo.textures[0]);
1477 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureInfo.maskTextureId);
1478 if(maskCacheIndex != INVALID_CACHE_INDEX)
1480 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
1481 if(maskTextureInfo.storageType == StorageType::UPLOAD_TO_TEXTURE || maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1483 if(!maskTextureInfo.textures.empty())
1485 textureSet.SetTexture(MASK_TEXTURE_INDEX, maskTextureInfo.textures[0]);
1494 void TextureManager::RemoveTextureObserver(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
1496 // Remove its observer
1499 const auto iterEnd = textureInfo.observerList.End();
1500 const auto iter = std::find(textureInfo.observerList.Begin(), iterEnd, observer);
1503 // Disconnect and remove the observer.
1504 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1505 textureInfo.observerList.Erase(iter);
1510 } // namespace Internal
1512 } // namespace Toolkit