2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "texture-manager-impl.h"
22 #include <dali/devel-api/adaptor-framework/environment-variable.h>
23 #include <dali/devel-api/adaptor-framework/image-loading.h>
24 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/public-api/rendering/geometry.h>
29 #include <dali-toolkit/internal/texture-manager/texture-async-loading-helper.h>
30 #include <dali-toolkit/internal/texture-manager/texture-cache-manager.h>
31 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
32 #include <dali-toolkit/internal/visuals/rendering-addon.h>
36 constexpr auto INITIAL_HASH_NUMBER = size_t{0u};
37 constexpr auto DEFAULT_NUMBER_OF_LOCAL_LOADER_THREADS = size_t{4u};
38 constexpr auto DEFAULT_NUMBER_OF_REMOTE_LOADER_THREADS = size_t{8u};
40 constexpr auto TEXTURE_INDEX = 0u; ///< The Index for texture
41 constexpr auto MASK_TEXTURE_INDEX = 1u; ///< The Index for mask texture
43 constexpr auto NUMBER_OF_LOCAL_LOADER_THREADS_ENV = "DALI_TEXTURE_LOCAL_THREADS";
44 constexpr auto NUMBER_OF_REMOTE_LOADER_THREADS_ENV = "DALI_TEXTURE_REMOTE_THREADS";
45 constexpr auto LOAD_IMAGE_YUV_PLANES_ENV = "DALI_LOAD_IMAGE_YUV_PLANES";
47 size_t GetNumberOfThreads(const char* environmentVariable, size_t defaultValue)
49 using Dali::EnvironmentVariable::GetEnvironmentVariable;
50 auto numberString = GetEnvironmentVariable(environmentVariable);
51 auto numberOfThreads = numberString ? std::strtoul(numberString, nullptr, 10) : 0;
52 constexpr auto MAX_NUMBER_OF_THREADS = 100u;
53 DALI_ASSERT_DEBUG(numberOfThreads < MAX_NUMBER_OF_THREADS);
54 return (numberOfThreads > 0 && numberOfThreads < MAX_NUMBER_OF_THREADS) ? numberOfThreads : defaultValue;
57 size_t GetNumberOfLocalLoaderThreads()
59 return GetNumberOfThreads(NUMBER_OF_LOCAL_LOADER_THREADS_ENV, DEFAULT_NUMBER_OF_LOCAL_LOADER_THREADS);
62 size_t GetNumberOfRemoteLoaderThreads()
64 return GetNumberOfThreads(NUMBER_OF_REMOTE_LOADER_THREADS_ENV, DEFAULT_NUMBER_OF_REMOTE_LOADER_THREADS);
67 bool NeedToLoadYuvPlanes()
69 auto loadYuvPlanesString = Dali::EnvironmentVariable::GetEnvironmentVariable(LOAD_IMAGE_YUV_PLANES_ENV);
70 bool loadYuvPlanes = loadYuvPlanesString ? std::atoi(loadYuvPlanesString) : false;
82 // Define logfilter Internal namespace level so other files (e.g. texture-cache-manager.cpp) can also use this filter.
83 Debug::Filter* gTextureManagerLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TEXTURE_MANAGER");
86 #define GET_LOAD_STATE_STRING(loadState) \
87 loadState == TextureManagerType::LoadState::NOT_STARTED ? "NOT_STARTED" : \
88 loadState == TextureManagerType::LoadState::LOADING ? "LOADING" : \
89 loadState == TextureManagerType::LoadState::LOAD_FINISHED ? "LOAD_FINISHED" : \
90 loadState == TextureManagerType::LoadState::WAITING_FOR_MASK ? "WAITING_FOR_MASK" : \
91 loadState == TextureManagerType::LoadState::MASK_APPLYING ? "MASK_APPLYING" : \
92 loadState == TextureManagerType::LoadState::MASK_APPLIED ? "MASK_APPLIED" : \
93 loadState == TextureManagerType::LoadState::UPLOADED ? "UPLOADED" : \
94 loadState == TextureManagerType::LoadState::CANCELLED ? "CANCELLED" : \
95 loadState == TextureManagerType::LoadState::MASK_CANCELLED ? "MASK_CANCELLED" : \
96 loadState == TextureManagerType::LoadState::LOAD_FAILED ? "LOAD_FAILED" : \
103 const uint32_t DEFAULT_ATLAS_SIZE(1024u); ///< This size can fit 8 by 8 images of average size 128 * 128
104 const Vector4 FULL_ATLAS_RECT(0.0f, 0.0f, 1.0f, 1.0f); ///< UV Rectangle that covers the full Texture
106 void PreMultiply(Devel::PixelBuffer pixelBuffer, TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
108 if(Pixel::HasAlpha(pixelBuffer.GetPixelFormat()))
110 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
112 pixelBuffer.MultiplyColorByAlpha();
117 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
121 } // Anonymous namespace
123 TextureManager::MaskingData::MaskingData()
125 mAlphaMaskId(INVALID_TEXTURE_ID),
126 mContentScaleFactor(1.0f),
128 mPreappliedMasking(true),
129 mMaskImageLoadingFailed(false)
133 TextureManager::TextureManager()
134 : mTextureCacheManager(),
135 mAsyncLocalLoaders(GetNumberOfLocalLoaderThreads(), [&]() { return TextureAsyncLoadingHelper(*this); }),
136 mAsyncRemoteLoaders(GetNumberOfRemoteLoaderThreads(), [&]() { return TextureAsyncLoadingHelper(*this); }),
137 mLifecycleObservers(),
140 mLoadingQueueTextureId(INVALID_TEXTURE_ID),
141 mLoadYuvPlanes(NeedToLoadYuvPlanes())
143 // Initialize the AddOn
144 RenderingAddOn::Get();
147 TextureManager::~TextureManager()
149 for(auto iter = mLifecycleObservers.Begin(), endIter = mLifecycleObservers.End(); iter != endIter; ++iter)
151 (*iter)->TextureManagerDestroyed();
155 TextureSet TextureManager::LoadAnimatedImageTexture(
156 const VisualUrl& url,
157 Dali::AnimatedImageLoading animatedImageLoading,
158 const uint32_t& frameIndex,
159 TextureManager::TextureId& textureId,
160 MaskingDataPointer& maskInfo,
161 const Dali::SamplingMode::Type& samplingMode,
162 const bool& synchronousLoading,
163 TextureUploadObserver* textureObserver,
164 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
166 TextureSet textureSet;
168 if(synchronousLoading)
170 Devel::PixelBuffer pixelBuffer;
171 if(animatedImageLoading)
173 pixelBuffer = animatedImageLoading.LoadFrame(frameIndex);
177 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous loading is failed\n");
182 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
184 Devel::PixelBuffer maskPixelBuffer = LoadImageFromFile(maskInfo->mAlphaMaskUrl.GetUrl(), ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true);
187 if(!maskInfo->mPreappliedMasking)
189 PixelData maskPixelData = Devel::PixelBuffer::Convert(maskPixelBuffer); // takes ownership of buffer
190 maskTexture = Texture::New(Dali::TextureType::TEXTURE_2D, maskPixelData.GetPixelFormat(), maskPixelData.GetWidth(), maskPixelData.GetHeight());
191 maskTexture.Upload(maskPixelData);
195 pixelBuffer.ApplyMask(maskPixelBuffer, maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
200 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous mask image loading is failed\n");
204 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
206 PreMultiply(pixelBuffer, preMultiplyOnLoad);
209 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
212 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight());
213 texture.Upload(pixelData);
214 textureSet = TextureSet::New();
215 textureSet.SetTexture(TEXTURE_INDEX, texture);
218 textureSet.SetTexture(MASK_TEXTURE_INDEX, maskTexture);
225 TextureId alphaMaskId = INVALID_TEXTURE_ID;
226 float contentScaleFactor = 1.0f;
227 bool cropToMask = false;
228 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
230 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, maskInfo->mPreappliedMasking ? StorageType::KEEP_PIXEL_BUFFER : StorageType::KEEP_TEXTURE);
231 alphaMaskId = maskInfo->mAlphaMaskId;
232 if(maskInfo->mPreappliedMasking)
234 contentScaleFactor = maskInfo->mContentScaleFactor;
235 cropToMask = maskInfo->mCropToMask;
239 textureId = RequestLoadInternal(url, alphaMaskId, contentScaleFactor, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR, UseAtlas::NO_ATLAS, cropToMask, StorageType::UPLOAD_TO_TEXTURE, textureObserver, true, TextureManager::ReloadPolicy::CACHED, preMultiplyOnLoad, animatedImageLoading, frameIndex, false);
241 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
242 if(loadState == TextureManager::LoadState::UPLOADED)
244 // LoadComplete has already been called - keep the same texture set
245 textureSet = GetTextureSet(textureId);
252 Devel::PixelBuffer TextureManager::LoadPixelBuffer(
253 const VisualUrl& url,
254 const Dali::ImageDimensions& desiredSize,
255 const Dali::FittingMode::Type& fittingMode,
256 const Dali::SamplingMode::Type& samplingMode,
257 const bool& synchronousLoading,
258 TextureUploadObserver* textureObserver,
259 const bool& orientationCorrection,
260 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
262 Devel::PixelBuffer pixelBuffer;
263 if(synchronousLoading)
267 if(url.IsBufferResource())
269 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
270 if(encodedImageBuffer)
272 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
277 pixelBuffer = LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
279 if(pixelBuffer && preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
281 PreMultiply(pixelBuffer, preMultiplyOnLoad);
287 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);
293 TextureSet TextureManager::LoadTexture(
294 const VisualUrl& url,
295 const Dali::ImageDimensions& desiredSize,
296 const Dali::FittingMode::Type& fittingMode,
297 const Dali::SamplingMode::Type& samplingMode,
298 MaskingDataPointer& maskInfo,
299 const bool& synchronousLoading,
300 TextureManager::TextureId& textureId,
301 Vector4& textureRect,
302 Dali::ImageDimensions& textureRectSize,
303 bool& atlasingStatus,
305 TextureUploadObserver* textureObserver,
306 AtlasUploadObserver* atlasObserver,
307 ImageAtlasManagerPtr imageAtlasManager,
308 const bool& orientationCorrection,
309 const TextureManager::ReloadPolicy& reloadPolicy,
310 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
312 TextureSet textureSet;
314 loadingStatus = false;
315 textureRect = FULL_ATLAS_RECT;
317 if(VisualUrl::TEXTURE == url.GetProtocolType())
319 std::string location = url.GetLocation();
320 if(location.size() > 0u)
322 TextureId id = std::stoi(location);
323 textureSet = mTextureCacheManager.GetExternalTextureSet(id);
326 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
335 if(synchronousLoading && atlasingStatus)
337 const bool synchronousAtlasAvaliable = (desiredSize != ImageDimensions() || url.IsLocalResource()) ? imageAtlasManager->CheckAtlasAvailable(url, desiredSize)
339 if(synchronousAtlasAvaliable)
341 std::vector<Devel::PixelBuffer> pixelBuffers;
342 LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection, false, pixelBuffers);
344 if(!pixelBuffers.empty() && maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
346 std::vector<Devel::PixelBuffer> maskPixelBuffers;
347 LoadImageSynchronously(maskInfo->mAlphaMaskUrl, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true, false, maskPixelBuffers);
348 if(!maskPixelBuffers.empty())
350 pixelBuffers[0].ApplyMask(maskPixelBuffers[0], maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
355 if(!pixelBuffers.empty())
357 PreMultiply(pixelBuffers[0], preMultiplyOnLoad);
358 data = Devel::PixelBuffer::Convert(pixelBuffers[0]); // takes ownership of buffer
362 textureSet = imageAtlasManager->Add(textureRect, data);
365 textureRectSize.SetWidth(data.GetWidth());
366 textureRectSize.SetHeight(data.GetHeight());
371 DALI_LOG_ERROR("TextureManager::LoadTexture: Synchronous Texture loading with atlasing is failed.\n");
376 atlasingStatus = false;
383 loadingStatus = true;
384 // Atlas manager can chage desired size when it is set by 0,0.
385 // We should store into textureRectSize only if atlasing successed.
386 // So copy inputed desiredSize, and replace value into textureRectSize only if atlasing success.
387 Dali::ImageDimensions atlasDesiredSize = desiredSize;
390 if(url.IsBufferResource())
392 const EncodedImageBuffer& encodedImageBuffer = GetEncodedImageBuffer(url.GetUrl());
393 if(encodedImageBuffer)
395 textureSet = imageAtlasManager->Add(textureRect, encodedImageBuffer, desiredSize, fittingMode, true, atlasObserver);
400 textureSet = imageAtlasManager->Add(textureRect, url, atlasDesiredSize, fittingMode, true, atlasObserver);
403 if(!textureSet) // big image, no atlasing or atlasing failed
405 atlasingStatus = false;
407 TextureId alphaMaskId = INVALID_TEXTURE_ID;
408 float contentScaleFactor = 1.0f;
409 bool cropToMask = false;
410 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
412 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, maskInfo->mPreappliedMasking ? StorageType::KEEP_PIXEL_BUFFER : StorageType::KEEP_TEXTURE, synchronousLoading);
413 alphaMaskId = maskInfo->mAlphaMaskId;
414 if(maskInfo && maskInfo->mPreappliedMasking)
416 contentScaleFactor = maskInfo->mContentScaleFactor;
417 cropToMask = maskInfo->mCropToMask;
421 textureId = RequestLoad(
431 orientationCorrection,
436 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
437 if(loadState == TextureManager::LoadState::UPLOADED)
439 // LoadComplete has already been called - keep the same texture set
440 textureSet = GetTextureSet(textureId);
443 // If we are loading the texture, or waiting for the ready signal handler to complete, inform
444 // caller that they need to wait.
445 loadingStatus = (loadState == TextureManager::LoadState::LOADING ||
446 loadState == TextureManager::LoadState::WAITING_FOR_MASK ||
447 loadState == TextureManager::LoadState::MASK_APPLYING ||
448 loadState == TextureManager::LoadState::MASK_APPLIED ||
449 loadState == TextureManager::LoadState::NOT_STARTED ||
450 mLoadingQueueTextureId != INVALID_TEXTURE_ID);
454 textureRectSize = atlasDesiredSize;
459 if(synchronousLoading)
461 loadingStatus = false;
467 TextureManager::TextureId TextureManager::RequestLoad(
468 const VisualUrl& url,
469 const ImageDimensions& desiredSize,
470 const Dali::FittingMode::Type& fittingMode,
471 const Dali::SamplingMode::Type& samplingMode,
472 const UseAtlas& useAtlas,
473 TextureUploadObserver* observer,
474 const bool& orientationCorrection,
475 const TextureManager::ReloadPolicy& reloadPolicy,
476 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
477 const bool& synchronousLoading)
479 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);
482 TextureManager::TextureId TextureManager::RequestLoad(
483 const VisualUrl& url,
484 const TextureManager::TextureId& maskTextureId,
485 const float& contentScale,
486 const Dali::ImageDimensions& desiredSize,
487 const Dali::FittingMode::Type& fittingMode,
488 const Dali::SamplingMode::Type& samplingMode,
489 const TextureManager::UseAtlas& useAtlas,
490 const bool& cropToMask,
491 TextureUploadObserver* observer,
492 const bool& orientationCorrection,
493 const TextureManager::ReloadPolicy& reloadPolicy,
494 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
495 const bool& synchronousLoading)
497 return RequestLoadInternal(url, maskTextureId, contentScale, desiredSize, fittingMode, samplingMode, useAtlas, cropToMask, StorageType::UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
500 TextureManager::TextureId TextureManager::RequestMaskLoad(
501 const VisualUrl& maskUrl,
502 StorageType storageType,
503 const bool& synchronousLoading)
505 // Use the normal load procedure to get the alpha mask.
506 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
507 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);
510 TextureManager::TextureId TextureManager::RequestLoadInternal(
511 const VisualUrl& url,
512 const TextureManager::TextureId& maskTextureId,
513 const float& contentScale,
514 const Dali::ImageDimensions& desiredSize,
515 const Dali::FittingMode::Type& fittingMode,
516 const Dali::SamplingMode::Type& samplingMode,
517 const TextureManager::UseAtlas& useAtlas,
518 const bool& cropToMask,
519 const TextureManager::StorageType& storageType,
520 TextureUploadObserver* observer,
521 const bool& orientationCorrection,
522 const TextureManager::ReloadPolicy& reloadPolicy,
523 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
524 Dali::AnimatedImageLoading animatedImageLoading,
525 const std::uint32_t& frameIndex,
526 const bool& synchronousLoading)
528 TextureHash textureHash = INITIAL_HASH_NUMBER;
529 TextureCacheIndex cacheIndex = INVALID_CACHE_INDEX;
530 bool loadYuvPlanes = (mLoadYuvPlanes && maskTextureId == INVALID_TEXTURE_ID && storageType == StorageType::UPLOAD_TO_TEXTURE);
532 if(storageType != StorageType::RETURN_PIXEL_BUFFER)
534 textureHash = mTextureCacheManager.GenerateHash(url, desiredSize, fittingMode, samplingMode, useAtlas, maskTextureId, cropToMask, frameIndex);
536 // Look up the texture by hash. Note: The extra parameters are used in case of a hash collision.
537 cacheIndex = mTextureCacheManager.FindCachedTexture(textureHash, url, desiredSize, fittingMode, samplingMode, useAtlas, storageType, maskTextureId, cropToMask, preMultiplyOnLoad, (animatedImageLoading) ? true : false, frameIndex);
540 TextureManager::TextureId textureId = INVALID_TEXTURE_ID;
541 // Check if the requested Texture exists in the cache.
542 if(cacheIndex != INVALID_CACHE_INDEX)
544 if(TextureManager::ReloadPolicy::CACHED == reloadPolicy)
546 // Mark this texture being used by another client resource. Forced reload would replace the current texture
547 // without the need for incrementing the reference count.
548 ++(mTextureCacheManager[cacheIndex].referenceCount);
550 textureId = mTextureCacheManager[cacheIndex].textureId;
552 // Update preMultiplyOnLoad value. It should be changed according to preMultiplied value of the cached info.
553 preMultiplyOnLoad = mTextureCacheManager[cacheIndex].preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
555 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);
558 if(textureId == INVALID_TEXTURE_ID) // There was no caching, or caching not required
560 textureId = mTextureCacheManager.GenerateTextureId();
562 bool preMultiply = (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
564 // Cache new texutre, and get cacheIndex.
565 cacheIndex = mTextureCacheManager.AppendCache(TextureInfo(textureId, maskTextureId, url, desiredSize, contentScale, fittingMode, samplingMode, false, cropToMask, useAtlas, textureHash, orientationCorrection, preMultiply, animatedImageLoading, frameIndex, loadYuvPlanes));
567 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);
570 // The below code path is common whether we are using the cache or not.
571 // The textureInfoIndex now refers to either a pre-existing cached TextureInfo,
572 // or a new TextureInfo just created.
573 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
574 textureInfo.maskTextureId = maskTextureId;
575 textureInfo.storageType = storageType;
576 textureInfo.orientationCorrection = orientationCorrection;
578 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureInfo loadState:%s\n", GET_LOAD_STATE_STRING(textureInfo.loadState));
580 // Force reloading of texture by setting loadState unless already loading or cancelled.
581 if(TextureManager::ReloadPolicy::FORCED == reloadPolicy &&
582 TextureManager::LoadState::LOADING != textureInfo.loadState &&
583 TextureManager::LoadState::WAITING_FOR_MASK != textureInfo.loadState &&
584 TextureManager::LoadState::MASK_APPLYING != textureInfo.loadState &&
585 TextureManager::LoadState::MASK_APPLIED != textureInfo.loadState &&
586 TextureManager::LoadState::CANCELLED != textureInfo.loadState &&
587 TextureManager::LoadState::MASK_CANCELLED != textureInfo.loadState)
589 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);
591 textureInfo.loadState = TextureManager::LoadState::NOT_STARTED;
594 if(!synchronousLoading)
596 // Check if we should add the observer.
597 // Only do this if we have not loaded yet and it will not have loaded by the end of this method.
598 switch(textureInfo.loadState)
600 case TextureManager::LoadState::LOAD_FAILED: // Failed notifies observer which then stops observing.
601 case TextureManager::LoadState::NOT_STARTED:
603 LoadOrQueueTexture(textureInfo, observer); // If called inside NotifyObservers, queues until afterwards
606 case TextureManager::LoadState::LOADING:
607 case TextureManager::LoadState::WAITING_FOR_MASK:
608 case TextureManager::LoadState::MASK_APPLYING:
609 case TextureManager::LoadState::MASK_APPLIED:
611 ObserveTexture(textureInfo, observer);
614 case TextureManager::LoadState::UPLOADED:
618 LoadOrQueueTexture(textureInfo, observer);
622 case TextureManager::LoadState::CANCELLED:
624 // A cancelled texture hasn't finished loading yet. Treat as a loading texture
625 // (it's ref count has already been incremented, above)
626 textureInfo.loadState = TextureManager::LoadState::LOADING;
627 ObserveTexture(textureInfo, observer);
630 case TextureManager::LoadState::MASK_CANCELLED:
632 // A cancelled texture hasn't finished mask applying yet. Treat as a mask applying texture
633 // (it's ref count has already been incremented, above)
634 textureInfo.loadState = TextureManager::LoadState::MASK_APPLYING;
635 ObserveTexture(textureInfo, observer);
638 case TextureManager::LoadState::LOAD_FINISHED:
640 // Loading has already completed.
641 if(observer && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
643 LoadOrQueueTexture(textureInfo, observer);
651 // If the image is already finished to load, use cached texture.
652 // We don't need to consider Observer because this is synchronous loading.
653 if(!(textureInfo.loadState == TextureManager::LoadState::UPLOADED ||
654 textureInfo.loadState == TextureManager::LoadState::LOAD_FINISHED))
656 std::vector<Devel::PixelBuffer> pixelBuffers;
657 LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection, loadYuvPlanes, pixelBuffers);
659 if(pixelBuffers.empty())
661 // If pixelBuffer loading is failed in synchronously, call Remove() method.
662 Remove(textureId, nullptr);
663 return INVALID_TEXTURE_ID;
666 if(storageType == StorageType::KEEP_PIXEL_BUFFER) // For the mask image loading.
668 textureInfo.pixelBuffer = pixelBuffers[0]; // Store the pixel data
669 textureInfo.loadState = LoadState::LOAD_FINISHED;
671 else // For the image loading.
674 if(maskTextureId != INVALID_TEXTURE_ID)
676 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
677 if(maskCacheIndex != INVALID_CACHE_INDEX)
679 if(mTextureCacheManager[maskCacheIndex].storageType == StorageType::KEEP_TEXTURE)
681 if(!mTextureCacheManager[maskCacheIndex].textures.empty())
683 maskTexture = mTextureCacheManager[maskCacheIndex].textures[0];
686 else if(mTextureCacheManager[maskCacheIndex].storageType == StorageType::KEEP_PIXEL_BUFFER)
688 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
691 pixelBuffers[0].ApplyMask(maskPixelBuffer, contentScale, cropToMask);
695 DALI_LOG_ERROR("Mask image cached invalid pixel buffer!\n");
701 DALI_LOG_ERROR("Mask image is not stored in cache.\n");
704 PreMultiply(pixelBuffers[0], preMultiplyOnLoad);
707 UploadTextures(pixelBuffers, textureInfo);
715 void TextureManager::Remove(const TextureManager::TextureId& textureId, TextureUploadObserver* observer)
717 if(textureId != INVALID_TEXTURE_ID)
719 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
720 if(textureCacheIndex != INVALID_CACHE_INDEX)
722 TextureManager::TextureId maskTextureId = INVALID_TEXTURE_ID;
723 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
724 // We only need to consider maskTextureId when texture's loadState is not cancelled. Because it is already deleted.
725 if(textureInfo.loadState != LoadState::CANCELLED && textureInfo.loadState != LoadState::MASK_CANCELLED)
727 if(textureInfo.maskTextureId != INVALID_TEXTURE_ID)
729 maskTextureId = textureInfo.maskTextureId;
733 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureManager::Remove( textureId=%d observer=%p ) cacheIndex:%d removal maskTextureId=%d, loadingQueueTextureId=%d, loadState=%s\n", textureId, observer, textureCacheIndex.GetIndex(), maskTextureId, mLoadingQueueTextureId, GET_LOAD_STATE_STRING(textureInfo.loadState));
735 // the case that LoadingQueue is working.
736 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
738 // If textureId is not same, this observer need to delete when ProcessRemoveQueue() is called.
739 // If textureId is same, we should not call RemoveTextureObserver.
740 // Because ObserverDestroyed signal already disconnected in NotifyObservers
741 TextureUploadObserver* queueObserver = observer;
742 if(mLoadingQueueTextureId == textureId)
744 queueObserver = nullptr;
747 // Remove element from the mLoadQueue
748 for(auto&& element : mLoadQueue)
750 if(element.mTextureId == textureId && element.mObserver == observer)
752 // Do not erase the item. We will clear it later in ProcessLoadQueue().
753 element.mTextureId = INVALID_TEXTURE_ID;
754 element.mObserver = nullptr;
759 mRemoveQueue.PushBack(QueueElement(textureId, queueObserver));
763 // Remove its observer
764 RemoveTextureObserver(textureInfo, observer);
766 // Remove textureId in CacheManager. Now, textureInfo is invalidate.
767 mTextureCacheManager.RemoveCache(textureInfo);
769 // Remove maskTextureId in CacheManager
770 if(maskTextureId != INVALID_TEXTURE_ID)
772 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
773 if(maskCacheIndex != INVALID_CACHE_INDEX)
775 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
776 mTextureCacheManager.RemoveCache(maskTextureInfo);
784 void TextureManager::LoadImageSynchronously(
785 const VisualUrl& url,
786 const Dali::ImageDimensions& desiredSize,
787 const Dali::FittingMode::Type& fittingMode,
788 const Dali::SamplingMode::Type& samplingMode,
789 const bool& orientationCorrection,
790 const bool& loadYuvPlanes,
791 std::vector<Devel::PixelBuffer>& pixelBuffers)
793 Devel::PixelBuffer pixelBuffer;
794 if(url.IsBufferResource())
796 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
797 if(encodedImageBuffer)
799 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
806 Dali::LoadImagePlanesFromFile(url.GetUrl(), pixelBuffers, desiredSize, fittingMode, samplingMode, orientationCorrection);
810 pixelBuffer = Dali::LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
816 pixelBuffers.push_back(pixelBuffer);
820 void TextureManager::AddObserver(TextureManager::LifecycleObserver& observer)
822 // make sure an observer doesn't observe the same object twice
823 // otherwise it will get multiple calls to ObjectDestroyed()
824 DALI_ASSERT_DEBUG(mLifecycleObservers.End() == std::find(mLifecycleObservers.Begin(), mLifecycleObservers.End(), &observer));
825 mLifecycleObservers.PushBack(&observer);
828 void TextureManager::RemoveObserver(TextureManager::LifecycleObserver& observer)
830 // Find the observer...
831 auto endIter = mLifecycleObservers.End();
832 for(auto iter = mLifecycleObservers.Begin(); iter != endIter; ++iter)
834 if((*iter) == &observer)
836 mLifecycleObservers.Erase(iter);
840 DALI_ASSERT_DEBUG(endIter != mLifecycleObservers.End());
843 void TextureManager::LoadOrQueueTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
845 switch(textureInfo.loadState)
847 case LoadState::NOT_STARTED:
848 case LoadState::LOAD_FAILED:
850 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
852 QueueLoadTexture(textureInfo, observer);
856 LoadTexture(textureInfo, observer);
860 case LoadState::UPLOADED:
862 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
864 QueueLoadTexture(textureInfo, observer);
868 // The Texture has already loaded. The other observers have already been notified.
869 // We need to send a "late" loaded notification for this observer.
870 EmitLoadComplete(observer, textureInfo, true);
874 case LoadState::LOADING:
875 case LoadState::CANCELLED:
876 case LoadState::MASK_CANCELLED:
877 case LoadState::LOAD_FINISHED:
878 case LoadState::WAITING_FOR_MASK:
879 case LoadState::MASK_APPLYING:
880 case LoadState::MASK_APPLIED:
887 void TextureManager::QueueLoadTexture(const TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
889 const auto& textureId = textureInfo.textureId;
890 mLoadQueue.PushBack(QueueElement(textureId, observer));
894 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
898 void TextureManager::LoadTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
900 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::LoadTexture(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
902 textureInfo.loadState = LoadState::LOADING;
903 if(!textureInfo.loadSynchronously)
905 auto& loadersContainer = (textureInfo.url.IsLocalResource() || textureInfo.url.IsBufferResource()) ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
906 auto loadingHelperIt = loadersContainer.GetNext();
907 auto premultiplyOnLoad = (textureInfo.preMultiplyOnLoad && textureInfo.maskTextureId == INVALID_TEXTURE_ID) ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
908 DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
909 if(textureInfo.animatedImageLoading)
911 loadingHelperIt->LoadAnimatedImage(textureInfo.textureId, textureInfo.animatedImageLoading, textureInfo.frameIndex, premultiplyOnLoad);
915 loadingHelperIt->Load(textureInfo.textureId, textureInfo.url, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, textureInfo.orientationCorrection, premultiplyOnLoad, textureInfo.loadYuvPlanes);
918 ObserveTexture(textureInfo, observer);
921 void TextureManager::ProcessLoadQueue()
923 for(auto&& element : mLoadQueue)
925 if(element.mTextureId == INVALID_TEXTURE_ID)
930 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(element.mTextureId);
931 if(cacheIndex != INVALID_CACHE_INDEX)
933 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
934 if((textureInfo.loadState == LoadState::UPLOADED) || (textureInfo.loadState == LoadState::LOAD_FINISHED && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER))
936 if(element.mObserver)
938 EmitLoadComplete(element.mObserver, textureInfo, true);
941 else if(textureInfo.loadState == LoadState::LOADING)
943 // Note : LOADING state texture cannot be queue.
944 // This case be occured when same texture id are queue in mLoadQueue.
945 ObserveTexture(textureInfo, element.mObserver);
949 LoadTexture(textureInfo, element.mObserver);
956 void TextureManager::ProcessRemoveQueue()
958 for(auto&& element : mRemoveQueue)
960 if(element.mTextureId != INVALID_TEXTURE_ID)
962 Remove(element.mTextureId, element.mObserver);
965 mRemoveQueue.Clear();
968 void TextureManager::ObserveTexture(TextureManager::TextureInfo& textureInfo,
969 TextureUploadObserver* observer)
971 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ObserveTexture(): url:%s observer:%p\n", textureInfo.url.GetUrl().c_str(), observer);
975 textureInfo.observerList.PushBack(observer);
976 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
980 void TextureManager::AsyncLoadComplete(const TextureManager::TextureId& textureId, std::vector<Devel::PixelBuffer>& pixelBuffers)
982 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
983 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::AsyncLoadComplete( textureId:%d CacheIndex:%d )\n", textureId, cacheIndex.GetIndex());
984 if(cacheIndex != INVALID_CACHE_INDEX)
986 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
988 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));
990 if(textureInfo.loadState != LoadState::CANCELLED && textureInfo.loadState != LoadState::MASK_CANCELLED)
992 // textureInfo can be invalidated after this call (as the mTextureInfoContainer may be modified)
993 PostLoad(textureInfo, pixelBuffers);
997 Remove(textureInfo.textureId, nullptr);
1002 void TextureManager::PostLoad(TextureManager::TextureInfo& textureInfo, std::vector<Devel::PixelBuffer>& pixelBuffers)
1004 // Was the load successful?
1005 if(!pixelBuffers.empty())
1007 if(pixelBuffers.size() == 1)
1009 Devel::PixelBuffer pixelBuffer = pixelBuffers[0];
1010 if(pixelBuffer && (pixelBuffer.GetWidth() != 0) && (pixelBuffer.GetHeight() != 0))
1012 // No atlas support for now
1013 textureInfo.useAtlas = UseAtlas::NO_ATLAS;
1014 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
1016 if(textureInfo.storageType == StorageType::UPLOAD_TO_TEXTURE)
1018 // If there is a mask texture ID associated with this texture, then apply the mask
1019 // if it's already loaded. If it hasn't, and the mask is still loading,
1020 // wait for the mask to finish loading.
1021 // note, If the texture is already uploaded synchronously during loading,
1022 // we don't need to apply mask.
1023 if(textureInfo.loadState != LoadState::UPLOADED &&
1024 textureInfo.maskTextureId != INVALID_TEXTURE_ID)
1026 if(textureInfo.loadState == LoadState::MASK_APPLYING)
1028 textureInfo.loadState = LoadState::MASK_APPLIED;
1029 UploadTextures(pixelBuffers, textureInfo);
1030 NotifyObservers(textureInfo, true);
1034 LoadState maskLoadState = mTextureCacheManager.GetTextureStateInternal(textureInfo.maskTextureId);
1035 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
1036 if(maskLoadState == LoadState::LOADING)
1038 textureInfo.loadState = LoadState::WAITING_FOR_MASK;
1040 else if(maskLoadState == LoadState::LOAD_FINISHED || maskLoadState == LoadState::UPLOADED)
1042 // Send New Task to Thread
1043 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureInfo.maskTextureId);
1044 if(maskCacheIndex != INVALID_CACHE_INDEX)
1046 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
1047 if(maskTextureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER)
1049 // Send New Task to Thread
1050 ApplyMask(textureInfo, textureInfo.maskTextureId);
1052 else if(maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1054 // Upload image texture. textureInfo.loadState will be UPLOADED.
1055 UploadTextures(pixelBuffers, textureInfo);
1057 // notify mask texture set.
1058 NotifyObservers(textureInfo, true);
1062 else // maskLoadState == LoadState::LOAD_FAILED
1064 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1065 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1066 UploadTextures(pixelBuffers, textureInfo);
1067 NotifyObservers(textureInfo, true);
1073 UploadTextures(pixelBuffers, textureInfo);
1074 NotifyObservers(textureInfo, true);
1079 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
1080 textureInfo.loadState = LoadState::LOAD_FINISHED;
1082 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1084 NotifyObservers(textureInfo, true);
1086 else // for the StorageType::KEEP_PIXEL_BUFFER and StorageType::KEEP_TEXTURE
1088 // Check if there was another texture waiting for this load to complete
1089 // (e.g. if this was an image mask, and its load is on a different thread)
1090 CheckForWaitingTexture(textureInfo);
1098 // No atlas support for now
1099 textureInfo.useAtlas = UseAtlas::NO_ATLAS;
1100 textureInfo.preMultiplied = false;
1102 UploadTextures(pixelBuffers, textureInfo);
1103 NotifyObservers(textureInfo, true);
1108 textureInfo.loadState = LoadState::LOAD_FAILED;
1109 if(textureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER || textureInfo.storageType == StorageType::KEEP_TEXTURE)
1111 // Check if there was another texture waiting for this load to complete
1112 // (e.g. if this was an image mask, and its load is on a different thread)
1113 CheckForWaitingTexture(textureInfo);
1117 NotifyObservers(textureInfo, false);
1122 void TextureManager::CheckForWaitingTexture(TextureManager::TextureInfo& maskTextureInfo)
1124 if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED &&
1125 maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1127 // Upload mask texture. textureInfo.loadState will be UPLOADED.
1128 std::vector<Devel::PixelBuffer> pixelBuffers;
1129 pixelBuffers.push_back(maskTextureInfo.pixelBuffer);
1130 UploadTextures(pixelBuffers, maskTextureInfo);
1133 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): maskTextureId=%d, maskTextureUrl=%s\n", maskTextureInfo.textureId, maskTextureInfo.url.GetUrl().c_str());
1135 // Search the cache, checking if any texture has this texture id as a maskTextureId
1136 const std::size_t size = mTextureCacheManager.size();
1138 // Keep notify observer required textureIds.
1139 // Note : NotifyObservers can change mTextureCacheManager cache struct. We should check id's validation before notify.
1140 std::vector<TextureId> notifyRequiredTextureIds;
1142 // TODO : Refactorize here to not iterate whole cached image.
1143 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1145 if(mTextureCacheManager[cacheIndex].maskTextureId == maskTextureInfo.textureId &&
1146 mTextureCacheManager[cacheIndex].loadState == LoadState::WAITING_FOR_MASK)
1148 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1150 if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED)
1152 if(maskTextureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER)
1154 // Send New Task to Thread
1155 ApplyMask(textureInfo, maskTextureInfo.textureId);
1158 else if(maskTextureInfo.loadState == LoadState::UPLOADED)
1160 if(maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1162 // Upload image texture. textureInfo.loadState will be UPLOADED.
1163 std::vector<Devel::PixelBuffer> pixelBuffers;
1164 pixelBuffers.push_back(textureInfo.pixelBuffer);
1165 UploadTextures(pixelBuffers, textureInfo);
1167 // Increase reference counts for notify required textureId.
1168 // Now we can assume that we don't remove & re-assign this textureId
1169 // during NotifyObserver signal emit.
1170 maskTextureInfo.referenceCount++;
1171 textureInfo.referenceCount++;
1173 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): Ready to notify textureId=%d\n", textureInfo.textureId);
1175 notifyRequiredTextureIds.push_back(textureInfo.textureId);
1178 else // maskTextureInfo.loadState == LoadState::LOAD_FAILED
1180 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1181 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1182 std::vector<Devel::PixelBuffer> pixelBuffers;
1183 pixelBuffers.push_back(textureInfo.pixelBuffer);
1184 UploadTextures(pixelBuffers, textureInfo);
1186 // Increase reference counts for notify required textureId.
1187 // Now we can assume that we don't remove & re-assign this textureId
1188 // during NotifyObserver signal emit.
1189 maskTextureInfo.referenceCount++;
1190 textureInfo.referenceCount++;
1192 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): Ready to notify textureId=%d\n", textureInfo.textureId);
1194 notifyRequiredTextureIds.push_back(textureInfo.textureId);
1199 // Notify textures are masked
1200 for(const auto textureId : notifyRequiredTextureIds)
1202 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1203 if(textureCacheIndex != INVALID_CACHE_INDEX)
1205 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
1206 NotifyObservers(textureInfo, true);
1210 // Decrease reference count
1211 for(const auto textureId : notifyRequiredTextureIds)
1213 Remove(textureId, nullptr);
1217 void TextureManager::ApplyMask(TextureManager::TextureInfo& textureInfo, const TextureManager::TextureId& maskTextureId)
1219 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
1220 if(maskCacheIndex != INVALID_CACHE_INDEX)
1222 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
1223 Devel::PixelBuffer pixelBuffer = textureInfo.pixelBuffer;
1224 textureInfo.pixelBuffer.Reset();
1226 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ApplyMask(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
1228 textureInfo.loadState = LoadState::MASK_APPLYING;
1229 auto& loadersContainer = (textureInfo.url.IsLocalResource() || textureInfo.url.IsBufferResource()) ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
1230 auto loadingHelperIt = loadersContainer.GetNext();
1231 auto premultiplyOnLoad = textureInfo.preMultiplyOnLoad ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
1232 DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
1233 loadingHelperIt->ApplyMask(textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask, premultiplyOnLoad);
1237 void TextureManager::UploadTextures(std::vector<Devel::PixelBuffer>& pixelBuffers, TextureManager::TextureInfo& textureInfo)
1239 if(!pixelBuffers.empty() && textureInfo.loadState != LoadState::UPLOADED && textureInfo.useAtlas != UseAtlas::USE_ATLAS)
1241 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, " TextureManager::UploadTextures() New Texture for textureId:%d\n", textureInfo.textureId);
1243 // Check if this pixelBuffer is premultiplied
1244 textureInfo.preMultiplied = pixelBuffers[0].IsAlphaPreMultiplied();
1246 auto& renderingAddOn = RenderingAddOn::Get();
1247 if(renderingAddOn.IsValid())
1249 renderingAddOn.CreateGeometry(textureInfo.textureId, pixelBuffers[0]);
1252 // Remove previous textures and insert new textures
1253 textureInfo.textures.clear();
1255 for(auto&& pixelBuffer : pixelBuffers)
1257 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight());
1258 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
1259 texture.Upload(pixelData);
1260 textureInfo.textures.push_back(texture);
1264 // Update the load state.
1265 // Note: This is regardless of success as we care about whether a
1266 // load attempt is in progress or not. If unsuccessful, a broken
1267 // image is still loaded.
1268 textureInfo.loadState = LoadState::UPLOADED;
1271 void TextureManager::NotifyObservers(TextureManager::TextureInfo& textureInfo, const bool& success)
1273 TextureId textureId = textureInfo.textureId;
1275 // If there is an observer: Notify the load is complete, whether successful or not,
1276 // and erase it from the list
1277 TextureInfo* info = &textureInfo;
1279 if(info->animatedImageLoading)
1281 // If loading failed, we don't need to get frameCount and frameInterval.
1284 info->frameCount = info->animatedImageLoading.GetImageCount();
1285 info->frameInterval = info->animatedImageLoading.GetFrameInterval(info->frameIndex);
1287 info->animatedImageLoading.Reset();
1290 mLoadingQueueTextureId = textureId;
1292 // Reverse observer list that we can pop_back the observer.
1293 std::reverse(info->observerList.Begin(), info->observerList.End());
1295 while(info->observerList.Count())
1297 TextureUploadObserver* observer = *(info->observerList.End() - 1u);
1299 // During LoadComplete() a Control ResourceReady() signal is emitted.
1300 // During that signal the app may add remove /add Textures (e.g. via
1302 // It is possible for observers to be removed from the observer list,
1303 // and it is also possible for the mTextureInfoContainer to be modified,
1304 // invalidating the reference to the textureInfo struct.
1305 // Texture load requests for the same URL are deferred until the end of this
1307 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));
1309 // It is possible for the observer to be deleted.
1310 // Disconnect and remove the observer first.
1311 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1313 info->observerList.Erase(info->observerList.End() - 1u);
1315 EmitLoadComplete(observer, *info, success);
1317 // Get the textureInfo from the container again as it may have been invalidated.
1318 TextureCacheIndex textureInfoIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1319 if(textureInfoIndex == INVALID_CACHE_INDEX)
1321 break; // texture has been removed - can stop.
1323 info = &mTextureCacheManager[textureInfoIndex];
1326 mLoadingQueueTextureId = INVALID_TEXTURE_ID;
1328 ProcessRemoveQueue();
1330 if(info->storageType == StorageType::RETURN_PIXEL_BUFFER && info->observerList.Count() == 0)
1332 Remove(info->textureId, nullptr);
1336 void TextureManager::ObserverDestroyed(TextureUploadObserver* observer)
1338 const std::size_t size = mTextureCacheManager.size();
1339 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1341 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1342 for(TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin();
1343 j != textureInfo.observerList.End();)
1347 j = textureInfo.observerList.Erase(j);
1356 // Remove element from the LoadQueue
1357 for(auto&& element : mLoadQueue)
1359 if(element.mObserver == observer)
1361 element.mTextureId = INVALID_TEXTURE_ID;
1362 element.mObserver = nullptr;
1367 Dali::Geometry TextureManager::GetRenderGeometry(const TextureManager::TextureId& textureId, std::uint32_t& frontElements, std::uint32_t& backElements)
1369 return RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().GetGeometry(textureId, frontElements, backElements) : Geometry();
1372 void TextureManager::EmitLoadComplete(TextureUploadObserver* observer, TextureManager::TextureInfo& textureInfo, const bool& success)
1374 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1376 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::PIXEL_BUFFER, textureInfo.pixelBuffer, textureInfo.url.GetUrl(), textureInfo.preMultiplied));
1380 TextureSet textureSet = GetTextureSet(textureInfo);
1381 if(textureInfo.isAnimatedImageFormat)
1383 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::ANIMATED_IMAGE_TEXTURE, textureInfo.textureId, textureSet, textureInfo.frameCount, textureInfo.frameInterval));
1387 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::TEXTURE, textureInfo.textureId, textureSet, (textureInfo.useAtlas == UseAtlas::USE_ATLAS) ? true : false, textureInfo.atlasRect, textureInfo.preMultiplied));
1392 TextureSet TextureManager::GetTextureSet(const TextureManager::TextureId& textureId)
1394 TextureSet textureSet;
1395 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
1396 if(loadState == TextureManager::LoadState::UPLOADED)
1398 // LoadComplete has already been called - keep the same texture set
1399 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1400 if(textureCacheIndex != INVALID_CACHE_INDEX)
1402 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
1403 textureSet = GetTextureSet(textureInfo);
1408 DALI_LOG_ERROR("GetTextureSet is failed. texture is not uploaded \n");
1413 TextureSet TextureManager::GetTextureSet(const TextureManager::TextureInfo& textureInfo)
1415 TextureSet textureSet;
1417 // LoadComplete has already been called - keep the same texture set
1418 textureSet = TextureSet::New();
1419 if(!textureInfo.textures.empty())
1421 if(textureInfo.textures.size() > 1) // For YUV case
1423 uint32_t index = 0u;
1424 for(auto&& texture : textureInfo.textures)
1426 textureSet.SetTexture(index++, texture);
1431 textureSet.SetTexture(TEXTURE_INDEX, textureInfo.textures[0]);
1432 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureInfo.maskTextureId);
1433 if(maskCacheIndex != INVALID_CACHE_INDEX)
1435 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
1436 if(maskTextureInfo.storageType == StorageType::UPLOAD_TO_TEXTURE || maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1438 if(!maskTextureInfo.textures.empty())
1440 textureSet.SetTexture(MASK_TEXTURE_INDEX, maskTextureInfo.textures[0]);
1449 void TextureManager::RemoveTextureObserver(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
1451 // Remove its observer
1454 const auto iterEnd = textureInfo.observerList.End();
1455 const auto iter = std::find(textureInfo.observerList.Begin(), iterEnd, observer);
1458 // Disconnect and remove the observer.
1459 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1460 textureInfo.observerList.Erase(iter);
1465 } // namespace Internal
1467 } // namespace Toolkit