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};
38 constexpr auto TEXTURE_INDEX = 0u; ///< The Index for texture
39 constexpr auto MASK_TEXTURE_INDEX = 1u; ///< The Index for mask texture
41 constexpr auto NUMBER_OF_LOCAL_LOADER_THREADS_ENV = "DALI_TEXTURE_LOCAL_THREADS";
42 constexpr auto NUMBER_OF_REMOTE_LOADER_THREADS_ENV = "DALI_TEXTURE_REMOTE_THREADS";
43 constexpr auto LOAD_IMAGE_YUV_PLANES_ENV = "DALI_LOAD_IMAGE_YUV_PLANES";
45 bool NeedToLoadYuvPlanes()
47 auto loadYuvPlanesString = Dali::EnvironmentVariable::GetEnvironmentVariable(LOAD_IMAGE_YUV_PLANES_ENV);
48 bool loadYuvPlanes = loadYuvPlanesString ? std::atoi(loadYuvPlanesString) : false;
60 // Define logfilter Internal namespace level so other files (e.g. texture-cache-manager.cpp) can also use this filter.
61 Debug::Filter* gTextureManagerLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TEXTURE_MANAGER");
64 #define GET_LOAD_STATE_STRING(loadState) \
65 loadState == TextureManagerType::LoadState::NOT_STARTED ? "NOT_STARTED" : \
66 loadState == TextureManagerType::LoadState::LOADING ? "LOADING" : \
67 loadState == TextureManagerType::LoadState::LOAD_FINISHED ? "LOAD_FINISHED" : \
68 loadState == TextureManagerType::LoadState::WAITING_FOR_MASK ? "WAITING_FOR_MASK" : \
69 loadState == TextureManagerType::LoadState::MASK_APPLYING ? "MASK_APPLYING" : \
70 loadState == TextureManagerType::LoadState::MASK_APPLIED ? "MASK_APPLIED" : \
71 loadState == TextureManagerType::LoadState::UPLOADED ? "UPLOADED" : \
72 loadState == TextureManagerType::LoadState::CANCELLED ? "CANCELLED" : \
73 loadState == TextureManagerType::LoadState::MASK_CANCELLED ? "MASK_CANCELLED" : \
74 loadState == TextureManagerType::LoadState::LOAD_FAILED ? "LOAD_FAILED" : \
81 const uint32_t DEFAULT_ATLAS_SIZE(1024u); ///< This size can fit 8 by 8 images of average size 128 * 128
82 const Vector4 FULL_ATLAS_RECT(0.0f, 0.0f, 1.0f, 1.0f); ///< UV Rectangle that covers the full Texture
84 void PreMultiply(Devel::PixelBuffer pixelBuffer, TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
86 if(Pixel::HasAlpha(pixelBuffer.GetPixelFormat()))
88 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
90 pixelBuffer.MultiplyColorByAlpha();
95 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
99 } // Anonymous namespace
101 TextureManager::MaskingData::MaskingData()
103 mAlphaMaskId(INVALID_TEXTURE_ID),
104 mContentScaleFactor(1.0f),
106 mPreappliedMasking(true),
107 mMaskImageLoadingFailed(false)
111 TextureManager::TextureManager()
112 : mTextureCacheManager(),
113 mAsyncLoader(std::unique_ptr<TextureAsyncLoadingHelper>(new TextureAsyncLoadingHelper(*this))),
114 mLifecycleObservers(),
117 mLoadingQueueTextureId(INVALID_TEXTURE_ID),
118 mLoadYuvPlanes(NeedToLoadYuvPlanes())
120 // Initialize the AddOn
121 RenderingAddOn::Get();
124 TextureManager::~TextureManager()
126 for(auto iter = mLifecycleObservers.Begin(), endIter = mLifecycleObservers.End(); iter != endIter; ++iter)
128 (*iter)->TextureManagerDestroyed();
132 TextureSet TextureManager::LoadAnimatedImageTexture(
133 const VisualUrl& url,
134 Dali::AnimatedImageLoading animatedImageLoading,
135 const uint32_t& frameIndex,
136 TextureManager::TextureId& textureId,
137 MaskingDataPointer& maskInfo,
138 const Dali::ImageDimensions& desiredSize,
139 const Dali::FittingMode::Type& fittingMode,
140 const Dali::SamplingMode::Type& samplingMode,
141 const bool& synchronousLoading,
142 TextureUploadObserver* textureObserver,
143 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
145 TextureSet textureSet;
147 if(synchronousLoading)
149 Devel::PixelBuffer pixelBuffer;
150 if(animatedImageLoading)
152 pixelBuffer = animatedImageLoading.LoadFrame(frameIndex, desiredSize, fittingMode, samplingMode);
156 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous loading is failed\n");
161 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
163 Devel::PixelBuffer maskPixelBuffer = LoadImageFromFile(maskInfo->mAlphaMaskUrl.GetUrl(), desiredSize, fittingMode, samplingMode, true);
166 if(!maskInfo->mPreappliedMasking)
168 PixelData maskPixelData = Devel::PixelBuffer::Convert(maskPixelBuffer); // takes ownership of buffer
169 maskTexture = Texture::New(Dali::TextureType::TEXTURE_2D, maskPixelData.GetPixelFormat(), maskPixelData.GetWidth(), maskPixelData.GetHeight());
170 maskTexture.Upload(maskPixelData);
174 pixelBuffer.ApplyMask(maskPixelBuffer, maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
179 DALI_LOG_ERROR("TextureManager::LoadAnimatedImageTexture: Synchronous mask image loading is failed\n");
183 if(preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
185 PreMultiply(pixelBuffer, preMultiplyOnLoad);
188 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
191 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight());
192 texture.Upload(pixelData);
193 textureSet = TextureSet::New();
194 textureSet.SetTexture(TEXTURE_INDEX, texture);
197 textureSet.SetTexture(MASK_TEXTURE_INDEX, maskTexture);
204 TextureId alphaMaskId = INVALID_TEXTURE_ID;
205 float contentScaleFactor = 1.0f;
206 bool cropToMask = false;
207 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
209 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, maskInfo->mPreappliedMasking ? StorageType::KEEP_PIXEL_BUFFER : StorageType::KEEP_TEXTURE);
210 alphaMaskId = maskInfo->mAlphaMaskId;
211 if(maskInfo->mPreappliedMasking)
213 contentScaleFactor = maskInfo->mContentScaleFactor;
214 cropToMask = maskInfo->mCropToMask;
218 textureId = RequestLoadInternal(url, alphaMaskId, contentScaleFactor, desiredSize, fittingMode, samplingMode, UseAtlas::NO_ATLAS, cropToMask, StorageType::UPLOAD_TO_TEXTURE, textureObserver, true, TextureManager::ReloadPolicy::CACHED, preMultiplyOnLoad, animatedImageLoading, frameIndex, false);
220 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
221 if(loadState == TextureManager::LoadState::UPLOADED)
223 // LoadComplete has already been called - keep the same texture set
224 textureSet = GetTextureSet(textureId);
231 Devel::PixelBuffer TextureManager::LoadPixelBuffer(
232 const VisualUrl& url,
233 const Dali::ImageDimensions& desiredSize,
234 const Dali::FittingMode::Type& fittingMode,
235 const Dali::SamplingMode::Type& samplingMode,
236 const bool& synchronousLoading,
237 TextureUploadObserver* textureObserver,
238 const bool& orientationCorrection,
239 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
241 Devel::PixelBuffer pixelBuffer;
242 if(synchronousLoading)
246 if(url.IsBufferResource())
248 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
249 if(encodedImageBuffer)
251 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
256 pixelBuffer = LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
258 if(pixelBuffer && preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD)
260 PreMultiply(pixelBuffer, preMultiplyOnLoad);
266 RequestLoadInternal(url, INVALID_TEXTURE_ID, 1.0f, desiredSize, fittingMode, samplingMode, UseAtlas::NO_ATLAS, false, StorageType::RETURN_PIXEL_BUFFER, textureObserver, orientationCorrection, TextureManager::ReloadPolicy::FORCED, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, false);
272 TextureSet TextureManager::LoadTexture(
273 const VisualUrl& url,
274 const Dali::ImageDimensions& desiredSize,
275 const Dali::FittingMode::Type& fittingMode,
276 const Dali::SamplingMode::Type& samplingMode,
277 MaskingDataPointer& maskInfo,
278 const bool& synchronousLoading,
279 TextureManager::TextureId& textureId,
280 Vector4& textureRect,
281 Dali::ImageDimensions& textureRectSize,
282 bool& atlasingStatus,
284 TextureUploadObserver* textureObserver,
285 AtlasUploadObserver* atlasObserver,
286 ImageAtlasManagerPtr imageAtlasManager,
287 const bool& orientationCorrection,
288 const TextureManager::ReloadPolicy& reloadPolicy,
289 TextureManager::MultiplyOnLoad& preMultiplyOnLoad)
291 TextureSet textureSet;
293 loadingStatus = false;
294 textureRect = FULL_ATLAS_RECT;
296 if(VisualUrl::TEXTURE == url.GetProtocolType())
298 std::string location = url.GetLocation();
299 if(location.size() > 0u)
301 TextureId id = std::stoi(location);
302 textureSet = mTextureCacheManager.GetExternalTextureSet(id);
305 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
314 if(synchronousLoading && atlasingStatus)
316 const bool synchronousAtlasAvaliable = (desiredSize != ImageDimensions() || url.IsLocalResource()) ? imageAtlasManager->CheckAtlasAvailable(url, desiredSize)
318 if(synchronousAtlasAvaliable)
320 std::vector<Devel::PixelBuffer> pixelBuffers;
321 LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection, false, pixelBuffers);
323 if(!pixelBuffers.empty() && maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
325 std::vector<Devel::PixelBuffer> maskPixelBuffers;
326 LoadImageSynchronously(maskInfo->mAlphaMaskUrl, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true, false, maskPixelBuffers);
327 if(!maskPixelBuffers.empty())
329 pixelBuffers[0].ApplyMask(maskPixelBuffers[0], maskInfo->mContentScaleFactor, maskInfo->mCropToMask);
334 if(!pixelBuffers.empty())
336 PreMultiply(pixelBuffers[0], preMultiplyOnLoad);
337 data = Devel::PixelBuffer::Convert(pixelBuffers[0]); // takes ownership of buffer
341 textureSet = imageAtlasManager->Add(textureRect, data);
344 textureRectSize.SetWidth(data.GetWidth());
345 textureRectSize.SetHeight(data.GetHeight());
350 DALI_LOG_ERROR("TextureManager::LoadTexture: Synchronous Texture loading with atlasing is failed.\n");
355 atlasingStatus = false;
362 loadingStatus = true;
363 // Atlas manager can chage desired size when it is set by 0,0.
364 // We should store into textureRectSize only if atlasing successed.
365 // So copy inputed desiredSize, and replace value into textureRectSize only if atlasing success.
366 Dali::ImageDimensions atlasDesiredSize = desiredSize;
369 if(url.IsBufferResource())
371 const EncodedImageBuffer& encodedImageBuffer = GetEncodedImageBuffer(url.GetUrl());
372 if(encodedImageBuffer)
374 textureSet = imageAtlasManager->Add(textureRect, encodedImageBuffer, desiredSize, fittingMode, true, atlasObserver);
379 textureSet = imageAtlasManager->Add(textureRect, url, atlasDesiredSize, fittingMode, true, atlasObserver);
382 if(!textureSet) // big image, no atlasing or atlasing failed
384 atlasingStatus = false;
386 TextureId alphaMaskId = INVALID_TEXTURE_ID;
387 float contentScaleFactor = 1.0f;
388 bool cropToMask = false;
389 if(maskInfo && maskInfo->mAlphaMaskUrl.IsValid())
391 maskInfo->mAlphaMaskId = RequestMaskLoad(maskInfo->mAlphaMaskUrl, maskInfo->mPreappliedMasking ? StorageType::KEEP_PIXEL_BUFFER : StorageType::KEEP_TEXTURE, synchronousLoading);
392 alphaMaskId = maskInfo->mAlphaMaskId;
393 if(maskInfo && maskInfo->mPreappliedMasking)
395 contentScaleFactor = maskInfo->mContentScaleFactor;
396 cropToMask = maskInfo->mCropToMask;
400 textureId = RequestLoad(
410 orientationCorrection,
415 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
416 if(loadState == TextureManager::LoadState::UPLOADED)
418 // LoadComplete has already been called - keep the same texture set
419 textureSet = GetTextureSet(textureId);
422 // If we are loading the texture, or waiting for the ready signal handler to complete, inform
423 // caller that they need to wait.
424 loadingStatus = (loadState == TextureManager::LoadState::LOADING ||
425 loadState == TextureManager::LoadState::WAITING_FOR_MASK ||
426 loadState == TextureManager::LoadState::MASK_APPLYING ||
427 loadState == TextureManager::LoadState::MASK_APPLIED ||
428 loadState == TextureManager::LoadState::NOT_STARTED ||
429 mLoadingQueueTextureId != INVALID_TEXTURE_ID);
433 textureRectSize = atlasDesiredSize;
438 if(synchronousLoading)
440 loadingStatus = false;
446 TextureManager::TextureId TextureManager::RequestLoad(
447 const VisualUrl& url,
448 const ImageDimensions& desiredSize,
449 const Dali::FittingMode::Type& fittingMode,
450 const Dali::SamplingMode::Type& samplingMode,
451 const UseAtlas& useAtlas,
452 TextureUploadObserver* observer,
453 const bool& orientationCorrection,
454 const TextureManager::ReloadPolicy& reloadPolicy,
455 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
456 const bool& synchronousLoading)
458 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);
461 TextureManager::TextureId TextureManager::RequestLoad(
462 const VisualUrl& url,
463 const TextureManager::TextureId& maskTextureId,
464 const float& contentScale,
465 const Dali::ImageDimensions& desiredSize,
466 const Dali::FittingMode::Type& fittingMode,
467 const Dali::SamplingMode::Type& samplingMode,
468 const TextureManager::UseAtlas& useAtlas,
469 const bool& cropToMask,
470 TextureUploadObserver* observer,
471 const bool& orientationCorrection,
472 const TextureManager::ReloadPolicy& reloadPolicy,
473 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
474 const bool& synchronousLoading)
476 return RequestLoadInternal(url, maskTextureId, contentScale, desiredSize, fittingMode, samplingMode, useAtlas, cropToMask, StorageType::UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy, preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u, synchronousLoading);
479 TextureManager::TextureId TextureManager::RequestMaskLoad(
480 const VisualUrl& maskUrl,
481 StorageType storageType,
482 const bool& synchronousLoading)
484 // Use the normal load procedure to get the alpha mask.
485 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
486 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);
489 TextureManager::TextureId TextureManager::RequestLoadInternal(
490 const VisualUrl& url,
491 const TextureManager::TextureId& maskTextureId,
492 const float& contentScale,
493 const Dali::ImageDimensions& desiredSize,
494 const Dali::FittingMode::Type& fittingMode,
495 const Dali::SamplingMode::Type& samplingMode,
496 const TextureManager::UseAtlas& useAtlas,
497 const bool& cropToMask,
498 const TextureManager::StorageType& storageType,
499 TextureUploadObserver* observer,
500 const bool& orientationCorrection,
501 const TextureManager::ReloadPolicy& reloadPolicy,
502 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
503 Dali::AnimatedImageLoading animatedImageLoading,
504 const std::uint32_t& frameIndex,
505 const bool& synchronousLoading)
507 TextureHash textureHash = INITIAL_HASH_NUMBER;
508 TextureCacheIndex cacheIndex = INVALID_CACHE_INDEX;
509 bool loadYuvPlanes = (mLoadYuvPlanes && maskTextureId == INVALID_TEXTURE_ID && storageType == StorageType::UPLOAD_TO_TEXTURE);
511 if(storageType != StorageType::RETURN_PIXEL_BUFFER)
513 textureHash = mTextureCacheManager.GenerateHash(url, desiredSize, fittingMode, samplingMode, useAtlas, maskTextureId, cropToMask, frameIndex);
515 // Look up the texture by hash. Note: The extra parameters are used in case of a hash collision.
516 cacheIndex = mTextureCacheManager.FindCachedTexture(textureHash, url, desiredSize, fittingMode, samplingMode, useAtlas, storageType, maskTextureId, cropToMask, preMultiplyOnLoad, (animatedImageLoading) ? true : false, frameIndex);
519 TextureManager::TextureId textureId = INVALID_TEXTURE_ID;
520 // Check if the requested Texture exists in the cache.
521 if(cacheIndex != INVALID_CACHE_INDEX)
523 if(TextureManager::ReloadPolicy::CACHED == reloadPolicy)
525 // Mark this texture being used by another client resource. Forced reload would replace the current texture
526 // without the need for incrementing the reference count.
527 ++(mTextureCacheManager[cacheIndex].referenceCount);
529 textureId = mTextureCacheManager[cacheIndex].textureId;
531 // Update preMultiplyOnLoad value. It should be changed according to preMultiplied value of the cached info.
532 preMultiplyOnLoad = mTextureCacheManager[cacheIndex].preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
533 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);
536 if(textureId == INVALID_TEXTURE_ID) // There was no caching, or caching not required
538 textureId = mTextureCacheManager.GenerateTextureId();
540 bool preMultiply = (preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD);
542 // Cache new texutre, and get cacheIndex.
543 cacheIndex = mTextureCacheManager.AppendCache(TextureInfo(textureId, maskTextureId, url, desiredSize, contentScale, fittingMode, samplingMode, false, cropToMask, useAtlas, textureHash, orientationCorrection, preMultiply, animatedImageLoading, frameIndex, loadYuvPlanes));
544 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);
547 // The below code path is common whether we are using the cache or not.
548 // The textureInfoIndex now refers to either a pre-existing cached TextureInfo,
549 // or a new TextureInfo just created.
550 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
551 textureInfo.maskTextureId = maskTextureId;
552 textureInfo.storageType = storageType;
553 textureInfo.orientationCorrection = orientationCorrection;
555 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, "TextureInfo loadState:%s\n", GET_LOAD_STATE_STRING(textureInfo.loadState));
557 // Force reloading of texture by setting loadState unless already loading or cancelled.
558 if(TextureManager::ReloadPolicy::FORCED == reloadPolicy &&
559 TextureManager::LoadState::LOADING != textureInfo.loadState &&
560 TextureManager::LoadState::WAITING_FOR_MASK != textureInfo.loadState &&
561 TextureManager::LoadState::MASK_APPLYING != textureInfo.loadState &&
562 TextureManager::LoadState::MASK_APPLIED != textureInfo.loadState &&
563 TextureManager::LoadState::CANCELLED != textureInfo.loadState &&
564 TextureManager::LoadState::MASK_CANCELLED != textureInfo.loadState)
566 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);
567 textureInfo.loadState = TextureManager::LoadState::NOT_STARTED;
570 if(!synchronousLoading)
572 // Check if we should add the observer.
573 // Only do this if we have not loaded yet and it will not have loaded by the end of this method.
574 switch(textureInfo.loadState)
576 case TextureManager::LoadState::LOAD_FAILED: // Failed notifies observer which then stops observing.
577 case TextureManager::LoadState::NOT_STARTED:
579 LoadOrQueueTexture(textureInfo, observer); // If called inside NotifyObservers, queues until afterwards
582 case TextureManager::LoadState::LOADING:
583 case TextureManager::LoadState::WAITING_FOR_MASK:
584 case TextureManager::LoadState::MASK_APPLYING:
585 case TextureManager::LoadState::MASK_APPLIED:
587 ObserveTexture(textureInfo, observer);
590 case TextureManager::LoadState::UPLOADED:
594 LoadOrQueueTexture(textureInfo, observer);
598 case TextureManager::LoadState::CANCELLED:
600 // A cancelled texture hasn't finished loading yet. Treat as a loading texture
601 // (it's ref count has already been incremented, above)
602 textureInfo.loadState = TextureManager::LoadState::LOADING;
603 ObserveTexture(textureInfo, observer);
606 case TextureManager::LoadState::MASK_CANCELLED:
608 // A cancelled texture hasn't finished mask applying yet. Treat as a mask applying texture
609 // (it's ref count has already been incremented, above)
610 textureInfo.loadState = TextureManager::LoadState::MASK_APPLYING;
611 ObserveTexture(textureInfo, observer);
614 case TextureManager::LoadState::LOAD_FINISHED:
616 // Loading has already completed.
617 if(observer && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
619 LoadOrQueueTexture(textureInfo, observer);
627 // If the image is already finished to load, use cached texture.
628 // We don't need to consider Observer because this is synchronous loading.
629 if(!(textureInfo.loadState == TextureManager::LoadState::UPLOADED ||
630 textureInfo.loadState == TextureManager::LoadState::LOAD_FINISHED))
632 std::vector<Devel::PixelBuffer> pixelBuffers;
633 LoadImageSynchronously(url, desiredSize, fittingMode, samplingMode, orientationCorrection, loadYuvPlanes, pixelBuffers);
635 if(pixelBuffers.empty())
637 // If pixelBuffer loading is failed in synchronously, call Remove() method.
638 Remove(textureId, nullptr);
639 return INVALID_TEXTURE_ID;
642 if(storageType == StorageType::KEEP_PIXEL_BUFFER) // For the mask image loading.
644 textureInfo.pixelBuffer = pixelBuffers[0]; // Store the pixel data
645 textureInfo.loadState = LoadState::LOAD_FINISHED;
647 else // For the image loading.
650 if(maskTextureId != INVALID_TEXTURE_ID)
652 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
653 if(maskCacheIndex != INVALID_CACHE_INDEX)
655 if(mTextureCacheManager[maskCacheIndex].storageType == StorageType::KEEP_TEXTURE)
657 if(!mTextureCacheManager[maskCacheIndex].textures.empty())
659 maskTexture = mTextureCacheManager[maskCacheIndex].textures[0];
662 else if(mTextureCacheManager[maskCacheIndex].storageType == StorageType::KEEP_PIXEL_BUFFER)
664 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
667 pixelBuffers[0].ApplyMask(maskPixelBuffer, contentScale, cropToMask);
671 DALI_LOG_ERROR("Mask image cached invalid pixel buffer!\n");
677 DALI_LOG_ERROR("Mask image is not stored in cache.\n");
680 PreMultiply(pixelBuffers[0], preMultiplyOnLoad);
683 UploadTextures(pixelBuffers, textureInfo);
691 void TextureManager::Remove(const TextureManager::TextureId& textureId, TextureUploadObserver* observer)
693 if(textureId != INVALID_TEXTURE_ID)
695 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
696 if(textureCacheIndex != INVALID_CACHE_INDEX)
698 TextureManager::TextureId maskTextureId = INVALID_TEXTURE_ID;
699 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
700 // We only need to consider maskTextureId when texture's loadState is not cancelled. Because it is already deleted.
701 if(textureInfo.loadState != LoadState::CANCELLED && textureInfo.loadState != LoadState::MASK_CANCELLED)
703 if(textureInfo.maskTextureId != INVALID_TEXTURE_ID)
705 maskTextureId = textureInfo.maskTextureId;
709 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));
711 // the case that LoadingQueue is working.
712 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
714 // If textureId is not same, this observer need to delete when ProcessRemoveQueue() is called.
715 // If textureId is same, we should not call RemoveTextureObserver.
716 // Because ObserverDestroyed signal already disconnected in NotifyObservers
717 TextureUploadObserver* queueObserver = observer;
718 if(mLoadingQueueTextureId == textureId)
720 queueObserver = nullptr;
723 // Remove element from the mLoadQueue
724 for(auto&& element : mLoadQueue)
726 if(element.mTextureId == textureId && element.mObserver == observer)
728 // Do not erase the item. We will clear it later in ProcessLoadQueue().
729 element.mTextureId = INVALID_TEXTURE_ID;
730 element.mObserver = nullptr;
735 mRemoveQueue.PushBack(QueueElement(textureId, queueObserver));
739 // Remove its observer
740 RemoveTextureObserver(textureInfo, observer);
742 // Remove textureId in CacheManager. Now, textureInfo is invalidate.
743 mTextureCacheManager.RemoveCache(textureInfo);
745 // Remove maskTextureId in CacheManager
746 if(maskTextureId != INVALID_TEXTURE_ID)
748 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
749 if(maskCacheIndex != INVALID_CACHE_INDEX)
751 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
752 mTextureCacheManager.RemoveCache(maskTextureInfo);
760 void TextureManager::LoadImageSynchronously(
761 const VisualUrl& url,
762 const Dali::ImageDimensions& desiredSize,
763 const Dali::FittingMode::Type& fittingMode,
764 const Dali::SamplingMode::Type& samplingMode,
765 const bool& orientationCorrection,
766 const bool& loadYuvPlanes,
767 std::vector<Devel::PixelBuffer>& pixelBuffers)
769 Devel::PixelBuffer pixelBuffer;
770 if(url.IsBufferResource())
772 const EncodedImageBuffer& encodedImageBuffer = mTextureCacheManager.GetEncodedImageBuffer(url);
773 if(encodedImageBuffer)
775 pixelBuffer = LoadImageFromBuffer(encodedImageBuffer.GetRawBuffer(), desiredSize, fittingMode, samplingMode, orientationCorrection);
782 Dali::LoadImagePlanesFromFile(url.GetUrl(), pixelBuffers, desiredSize, fittingMode, samplingMode, orientationCorrection);
786 pixelBuffer = Dali::LoadImageFromFile(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection);
792 pixelBuffers.push_back(pixelBuffer);
796 void TextureManager::AddObserver(TextureManager::LifecycleObserver& observer)
798 // make sure an observer doesn't observe the same object twice
799 // otherwise it will get multiple calls to ObjectDestroyed()
800 DALI_ASSERT_DEBUG(mLifecycleObservers.End() == std::find(mLifecycleObservers.Begin(), mLifecycleObservers.End(), &observer));
801 mLifecycleObservers.PushBack(&observer);
804 void TextureManager::RemoveObserver(TextureManager::LifecycleObserver& observer)
806 // Find the observer...
807 auto endIter = mLifecycleObservers.End();
808 for(auto iter = mLifecycleObservers.Begin(); iter != endIter; ++iter)
810 if((*iter) == &observer)
812 mLifecycleObservers.Erase(iter);
816 DALI_ASSERT_DEBUG(endIter != mLifecycleObservers.End());
819 void TextureManager::LoadOrQueueTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
821 switch(textureInfo.loadState)
823 case LoadState::NOT_STARTED:
824 case LoadState::LOAD_FAILED:
826 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
828 QueueLoadTexture(textureInfo, observer);
832 LoadTexture(textureInfo, observer);
836 case LoadState::UPLOADED:
838 if(mLoadingQueueTextureId != INVALID_TEXTURE_ID)
840 QueueLoadTexture(textureInfo, observer);
844 // The Texture has already loaded. The other observers have already been notified.
845 // We need to send a "late" loaded notification for this observer.
846 EmitLoadComplete(observer, textureInfo, true);
850 case LoadState::LOADING:
851 case LoadState::CANCELLED:
852 case LoadState::MASK_CANCELLED:
853 case LoadState::LOAD_FINISHED:
854 case LoadState::WAITING_FOR_MASK:
855 case LoadState::MASK_APPLYING:
856 case LoadState::MASK_APPLIED:
863 void TextureManager::QueueLoadTexture(const TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
865 const auto& textureId = textureInfo.textureId;
866 mLoadQueue.PushBack(QueueElement(textureId, observer));
870 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
874 void TextureManager::LoadTexture(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
876 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::LoadTexture(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
877 textureInfo.loadState = LoadState::LOADING;
878 if(!textureInfo.loadSynchronously)
880 auto premultiplyOnLoad = (textureInfo.preMultiplyOnLoad && textureInfo.maskTextureId == INVALID_TEXTURE_ID) ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
881 if(textureInfo.animatedImageLoading)
883 mAsyncLoader->LoadAnimatedImage(textureInfo.textureId, textureInfo.animatedImageLoading, textureInfo.frameIndex, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, premultiplyOnLoad);
887 mAsyncLoader->Load(textureInfo.textureId, textureInfo.url, textureInfo.desiredSize, textureInfo.fittingMode, textureInfo.samplingMode, textureInfo.orientationCorrection, premultiplyOnLoad, textureInfo.loadYuvPlanes);
890 ObserveTexture(textureInfo, observer);
893 void TextureManager::ProcessLoadQueue()
895 for(auto&& element : mLoadQueue)
897 if(element.mTextureId == INVALID_TEXTURE_ID)
902 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(element.mTextureId);
903 if(cacheIndex != INVALID_CACHE_INDEX)
905 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
906 if((textureInfo.loadState == LoadState::UPLOADED) || (textureInfo.loadState == LoadState::LOAD_FINISHED && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER))
908 if(element.mObserver)
910 EmitLoadComplete(element.mObserver, textureInfo, true);
913 else if(textureInfo.loadState == LoadState::LOADING)
915 // Note : LOADING state texture cannot be queue.
916 // This case be occured when same texture id are queue in mLoadQueue.
917 ObserveTexture(textureInfo, element.mObserver);
921 LoadTexture(textureInfo, element.mObserver);
928 void TextureManager::ProcessRemoveQueue()
930 for(auto&& element : mRemoveQueue)
932 if(element.mTextureId != INVALID_TEXTURE_ID)
934 Remove(element.mTextureId, element.mObserver);
937 mRemoveQueue.Clear();
940 void TextureManager::ObserveTexture(TextureManager::TextureInfo& textureInfo,
941 TextureUploadObserver* observer)
943 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ObserveTexture(): url:%s observer:%p\n", textureInfo.url.GetUrl().c_str(), observer);
947 textureInfo.observerList.PushBack(observer);
948 observer->DestructionSignal().Connect(this, &TextureManager::ObserverDestroyed);
952 void TextureManager::AsyncLoadComplete(const TextureManager::TextureId& textureId, std::vector<Devel::PixelBuffer>& pixelBuffers)
954 TextureCacheIndex cacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
955 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::AsyncLoadComplete( textureId:%d CacheIndex:%d )\n", textureId, cacheIndex.GetIndex());
956 if(cacheIndex != INVALID_CACHE_INDEX)
958 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
960 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));
961 if(textureInfo.loadState != LoadState::CANCELLED && textureInfo.loadState != LoadState::MASK_CANCELLED)
963 // textureInfo can be invalidated after this call (as the mTextureInfoContainer may be modified)
964 PostLoad(textureInfo, pixelBuffers);
968 Remove(textureInfo.textureId, nullptr);
973 void TextureManager::PostLoad(TextureManager::TextureInfo& textureInfo, std::vector<Devel::PixelBuffer>& pixelBuffers)
975 // Was the load successful?
976 if(!pixelBuffers.empty())
978 if(pixelBuffers.size() == 1)
980 Devel::PixelBuffer pixelBuffer = pixelBuffers[0];
981 if(pixelBuffer && (pixelBuffer.GetWidth() != 0) && (pixelBuffer.GetHeight() != 0))
983 // No atlas support for now
984 textureInfo.useAtlas = UseAtlas::NO_ATLAS;
985 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
987 if(textureInfo.storageType == StorageType::UPLOAD_TO_TEXTURE)
989 // If there is a mask texture ID associated with this texture, then apply the mask
990 // if it's already loaded. If it hasn't, and the mask is still loading,
991 // wait for the mask to finish loading.
992 // note, If the texture is already uploaded synchronously during loading,
993 // we don't need to apply mask.
994 if(textureInfo.loadState != LoadState::UPLOADED &&
995 textureInfo.maskTextureId != INVALID_TEXTURE_ID)
997 if(textureInfo.loadState == LoadState::MASK_APPLYING)
999 textureInfo.loadState = LoadState::MASK_APPLIED;
1000 UploadTextures(pixelBuffers, textureInfo);
1001 NotifyObservers(textureInfo, true);
1005 LoadState maskLoadState = mTextureCacheManager.GetTextureStateInternal(textureInfo.maskTextureId);
1006 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
1007 if(maskLoadState == LoadState::LOADING)
1009 textureInfo.loadState = LoadState::WAITING_FOR_MASK;
1011 else if(maskLoadState == LoadState::LOAD_FINISHED || maskLoadState == LoadState::UPLOADED)
1013 // Send New Task to Thread
1014 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureInfo.maskTextureId);
1015 if(maskCacheIndex != INVALID_CACHE_INDEX)
1017 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
1018 if(maskTextureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER)
1020 // Send New Task to Thread
1021 ApplyMask(textureInfo, textureInfo.maskTextureId);
1023 else if(maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1025 // Upload image texture. textureInfo.loadState will be UPLOADED.
1026 UploadTextures(pixelBuffers, textureInfo);
1028 // notify mask texture set.
1029 NotifyObservers(textureInfo, true);
1033 else // maskLoadState == LoadState::LOAD_FAILED
1035 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1036 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1037 UploadTextures(pixelBuffers, textureInfo);
1038 NotifyObservers(textureInfo, true);
1044 UploadTextures(pixelBuffers, textureInfo);
1045 NotifyObservers(textureInfo, true);
1050 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
1051 textureInfo.loadState = LoadState::LOAD_FINISHED;
1053 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1055 NotifyObservers(textureInfo, true);
1057 else // for the StorageType::KEEP_PIXEL_BUFFER and StorageType::KEEP_TEXTURE
1059 // Check if there was another texture waiting for this load to complete
1060 // (e.g. if this was an image mask, and its load is on a different thread)
1061 CheckForWaitingTexture(textureInfo);
1069 // No atlas support for now
1070 textureInfo.useAtlas = UseAtlas::NO_ATLAS;
1071 textureInfo.preMultiplied = false;
1073 UploadTextures(pixelBuffers, textureInfo);
1074 NotifyObservers(textureInfo, true);
1079 textureInfo.loadState = LoadState::LOAD_FAILED;
1080 if(textureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER || textureInfo.storageType == StorageType::KEEP_TEXTURE)
1082 // Check if there was another texture waiting for this load to complete
1083 // (e.g. if this was an image mask, and its load is on a different thread)
1084 CheckForWaitingTexture(textureInfo);
1088 NotifyObservers(textureInfo, false);
1093 void TextureManager::CheckForWaitingTexture(TextureManager::TextureInfo& maskTextureInfo)
1095 if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED &&
1096 maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1098 // Upload mask texture. textureInfo.loadState will be UPLOADED.
1099 std::vector<Devel::PixelBuffer> pixelBuffers;
1100 pixelBuffers.push_back(maskTextureInfo.pixelBuffer);
1101 UploadTextures(pixelBuffers, maskTextureInfo);
1104 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): maskTextureId=%d, maskTextureUrl=%s\n", maskTextureInfo.textureId, maskTextureInfo.url.GetUrl().c_str());
1106 // Search the cache, checking if any texture has this texture id as a maskTextureId
1107 const std::size_t size = mTextureCacheManager.size();
1109 // Keep notify observer required textureIds.
1110 // Note : NotifyObservers can change mTextureCacheManager cache struct. We should check id's validation before notify.
1111 std::vector<TextureId> notifyRequiredTextureIds;
1113 // TODO : Refactorize here to not iterate whole cached image.
1114 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1116 if(mTextureCacheManager[cacheIndex].maskTextureId == maskTextureInfo.textureId &&
1117 mTextureCacheManager[cacheIndex].loadState == LoadState::WAITING_FOR_MASK)
1119 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1121 if(maskTextureInfo.loadState == LoadState::LOAD_FINISHED)
1123 if(maskTextureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER)
1125 // Send New Task to Thread
1126 ApplyMask(textureInfo, maskTextureInfo.textureId);
1129 else if(maskTextureInfo.loadState == LoadState::UPLOADED)
1131 if(maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1133 // Upload image texture. textureInfo.loadState will be UPLOADED.
1134 std::vector<Devel::PixelBuffer> pixelBuffers;
1135 pixelBuffers.push_back(textureInfo.pixelBuffer);
1136 UploadTextures(pixelBuffers, textureInfo);
1138 // Increase reference counts for notify required textureId.
1139 // Now we can assume that we don't remove & re-assign this textureId
1140 // during NotifyObserver signal emit.
1141 maskTextureInfo.referenceCount++;
1142 textureInfo.referenceCount++;
1144 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): Ready to notify textureId=%d\n", textureInfo.textureId);
1146 notifyRequiredTextureIds.push_back(textureInfo.textureId);
1149 else // maskTextureInfo.loadState == LoadState::LOAD_FAILED
1151 // Url texture load success, But alpha mask texture load failed. Run as normal image upload.
1152 DALI_LOG_ERROR("Alpha mask image loading failed! Image will not be masked\n");
1153 std::vector<Devel::PixelBuffer> pixelBuffers;
1154 pixelBuffers.push_back(textureInfo.pixelBuffer);
1155 UploadTextures(pixelBuffers, textureInfo);
1157 // Increase reference counts for notify required textureId.
1158 // Now we can assume that we don't remove & re-assign this textureId
1159 // during NotifyObserver signal emit.
1160 maskTextureInfo.referenceCount++;
1161 textureInfo.referenceCount++;
1163 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::CheckForWaitingTexture(): Ready to notify textureId=%d\n", textureInfo.textureId);
1165 notifyRequiredTextureIds.push_back(textureInfo.textureId);
1170 // Notify textures are masked
1171 for(const auto textureId : notifyRequiredTextureIds)
1173 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1174 if(textureCacheIndex != INVALID_CACHE_INDEX)
1176 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
1177 NotifyObservers(textureInfo, true);
1181 // Decrease reference count
1182 for(const auto textureId : notifyRequiredTextureIds)
1184 Remove(textureId, nullptr);
1188 void TextureManager::ApplyMask(TextureManager::TextureInfo& textureInfo, const TextureManager::TextureId& maskTextureId)
1190 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(maskTextureId);
1191 if(maskCacheIndex != INVALID_CACHE_INDEX)
1193 Devel::PixelBuffer maskPixelBuffer = mTextureCacheManager[maskCacheIndex].pixelBuffer;
1194 Devel::PixelBuffer pixelBuffer = textureInfo.pixelBuffer;
1195 textureInfo.pixelBuffer.Reset();
1197 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::Concise, "TextureManager::ApplyMask(): url:%s sync:%s\n", textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously ? "T" : "F");
1199 textureInfo.loadState = LoadState::MASK_APPLYING;
1200 auto premultiplyOnLoad = textureInfo.preMultiplyOnLoad ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
1201 mAsyncLoader->ApplyMask(textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask, premultiplyOnLoad);
1205 void TextureManager::UploadTextures(std::vector<Devel::PixelBuffer>& pixelBuffers, TextureManager::TextureInfo& textureInfo)
1207 if(!pixelBuffers.empty() && textureInfo.loadState != LoadState::UPLOADED && textureInfo.useAtlas != UseAtlas::USE_ATLAS)
1209 DALI_LOG_INFO(gTextureManagerLogFilter, Debug::General, " TextureManager::UploadTextures() New Texture for textureId:%d\n", textureInfo.textureId);
1211 // Check if this pixelBuffer is premultiplied
1212 textureInfo.preMultiplied = pixelBuffers[0].IsAlphaPreMultiplied();
1214 auto& renderingAddOn = RenderingAddOn::Get();
1215 if(renderingAddOn.IsValid())
1217 renderingAddOn.CreateGeometry(textureInfo.textureId, pixelBuffers[0]);
1220 // Remove previous textures and insert new textures
1221 textureInfo.textures.clear();
1223 for(auto&& pixelBuffer : pixelBuffers)
1225 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight());
1226 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
1227 texture.Upload(pixelData);
1228 textureInfo.textures.push_back(texture);
1232 // Update the load state.
1233 // Note: This is regardless of success as we care about whether a
1234 // load attempt is in progress or not. If unsuccessful, a broken
1235 // image is still loaded.
1236 textureInfo.loadState = LoadState::UPLOADED;
1239 void TextureManager::NotifyObservers(TextureManager::TextureInfo& textureInfo, const bool& success)
1241 TextureId textureId = textureInfo.textureId;
1243 // If there is an observer: Notify the load is complete, whether successful or not,
1244 // and erase it from the list
1245 TextureInfo* info = &textureInfo;
1247 if(info->animatedImageLoading)
1249 // If loading failed, we don't need to get frameCount and frameInterval.
1252 info->frameCount = info->animatedImageLoading.GetImageCount();
1253 info->frameInterval = info->animatedImageLoading.GetFrameInterval(info->frameIndex);
1255 info->animatedImageLoading.Reset();
1258 mLoadingQueueTextureId = textureId;
1260 // Reverse observer list that we can pop_back the observer.
1261 std::reverse(info->observerList.Begin(), info->observerList.End());
1263 while(info->observerList.Count())
1265 TextureUploadObserver* observer = *(info->observerList.End() - 1u);
1267 // During LoadComplete() a Control ResourceReady() signal is emitted.
1268 // During that signal the app may add remove /add Textures (e.g. via
1270 // It is possible for observers to be removed from the observer list,
1271 // and it is also possible for the mTextureInfoContainer to be modified,
1272 // invalidating the reference to the textureInfo struct.
1273 // Texture load requests for the same URL are deferred until the end of this
1275 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));
1276 // It is possible for the observer to be deleted.
1277 // Disconnect and remove the observer first.
1278 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1280 info->observerList.Erase(info->observerList.End() - 1u);
1282 EmitLoadComplete(observer, *info, success);
1284 // Get the textureInfo from the container again as it may have been invalidated.
1285 TextureCacheIndex textureInfoIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1286 if(textureInfoIndex == INVALID_CACHE_INDEX)
1288 break; // texture has been removed - can stop.
1290 info = &mTextureCacheManager[textureInfoIndex];
1293 mLoadingQueueTextureId = INVALID_TEXTURE_ID;
1295 ProcessRemoveQueue();
1297 if(info->storageType == StorageType::RETURN_PIXEL_BUFFER && info->observerList.Count() == 0)
1299 Remove(info->textureId, nullptr);
1303 void TextureManager::ObserverDestroyed(TextureUploadObserver* observer)
1305 const std::size_t size = mTextureCacheManager.size();
1306 for(TextureCacheIndex cacheIndex = TextureCacheIndex(TextureManagerType::TEXTURE_CACHE_INDEX_TYPE_LOCAL, 0u); cacheIndex.GetIndex() < size; ++cacheIndex.detailValue.index)
1308 TextureInfo& textureInfo(mTextureCacheManager[cacheIndex]);
1309 for(TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin();
1310 j != textureInfo.observerList.End();)
1314 j = textureInfo.observerList.Erase(j);
1323 // Remove element from the LoadQueue
1324 for(auto&& element : mLoadQueue)
1326 if(element.mObserver == observer)
1328 element.mTextureId = INVALID_TEXTURE_ID;
1329 element.mObserver = nullptr;
1334 Dali::Geometry TextureManager::GetRenderGeometry(const TextureManager::TextureId& textureId, std::uint32_t& frontElements, std::uint32_t& backElements)
1336 return RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().GetGeometry(textureId, frontElements, backElements) : Geometry();
1339 void TextureManager::EmitLoadComplete(TextureUploadObserver* observer, TextureManager::TextureInfo& textureInfo, const bool& success)
1341 if(textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER)
1343 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::PIXEL_BUFFER, textureInfo.pixelBuffer, textureInfo.url.GetUrl(), textureInfo.preMultiplied));
1347 TextureSet textureSet = GetTextureSet(textureInfo);
1348 if(textureInfo.isAnimatedImageFormat)
1350 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::ANIMATED_IMAGE_TEXTURE, textureInfo.textureId, textureSet, textureInfo.frameCount, textureInfo.frameInterval));
1354 observer->LoadComplete(success, TextureUploadObserver::TextureInformation(TextureUploadObserver::ReturnType::TEXTURE, textureInfo.textureId, textureSet, (textureInfo.useAtlas == UseAtlas::USE_ATLAS) ? true : false, textureInfo.atlasRect, textureInfo.preMultiplied));
1359 TextureSet TextureManager::GetTextureSet(const TextureManager::TextureId& textureId)
1361 TextureSet textureSet;
1362 TextureManager::LoadState loadState = mTextureCacheManager.GetTextureStateInternal(textureId);
1363 if(loadState == TextureManager::LoadState::UPLOADED)
1365 // LoadComplete has already been called - keep the same texture set
1366 TextureCacheIndex textureCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureId);
1367 if(textureCacheIndex != INVALID_CACHE_INDEX)
1369 TextureInfo& textureInfo(mTextureCacheManager[textureCacheIndex]);
1370 textureSet = GetTextureSet(textureInfo);
1375 DALI_LOG_ERROR("GetTextureSet is failed. texture is not uploaded \n");
1380 TextureSet TextureManager::GetTextureSet(const TextureManager::TextureInfo& textureInfo)
1382 TextureSet textureSet;
1384 // LoadComplete has already been called - keep the same texture set
1385 textureSet = TextureSet::New();
1386 if(!textureInfo.textures.empty())
1388 if(textureInfo.textures.size() > 1) // For YUV case
1390 uint32_t index = 0u;
1391 for(auto&& texture : textureInfo.textures)
1393 textureSet.SetTexture(index++, texture);
1398 textureSet.SetTexture(TEXTURE_INDEX, textureInfo.textures[0]);
1399 TextureCacheIndex maskCacheIndex = mTextureCacheManager.GetCacheIndexFromId(textureInfo.maskTextureId);
1400 if(maskCacheIndex != INVALID_CACHE_INDEX)
1402 TextureInfo& maskTextureInfo(mTextureCacheManager[maskCacheIndex]);
1403 if(maskTextureInfo.storageType == StorageType::UPLOAD_TO_TEXTURE || maskTextureInfo.storageType == StorageType::KEEP_TEXTURE)
1405 if(!maskTextureInfo.textures.empty())
1407 textureSet.SetTexture(MASK_TEXTURE_INDEX, maskTextureInfo.textures[0]);
1416 void TextureManager::RemoveTextureObserver(TextureManager::TextureInfo& textureInfo, TextureUploadObserver* observer)
1418 // Remove its observer
1421 const auto iterEnd = textureInfo.observerList.End();
1422 const auto iter = std::find(textureInfo.observerList.Begin(), iterEnd, observer);
1425 // Disconnect and remove the observer.
1426 observer->DestructionSignal().Disconnect(this, &TextureManager::ObserverDestroyed);
1427 textureInfo.observerList.Erase(iter);
1432 } // namespace Internal
1434 } // namespace Toolkit