2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/visuals/texture-manager-impl.h>
24 #include <dali/public-api/math/vector4.h>
25 #include <dali/devel-api/adaptor-framework/environment-variable.h>
26 #include <dali/devel-api/adaptor-framework/image-loading.h>
27 #include <dali/devel-api/common/hash.h>
28 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
29 #include <dali/integration-api/debug.h>
30 #include <dali/public-api/rendering/geometry.h>
33 #include <dali-toolkit/internal/image-loader/image-atlas-impl.h>
34 #include <dali-toolkit/public-api/image-loader/sync-image-loader.h>
35 #include <dali-toolkit/internal/visuals/image-atlas-manager.h>
36 #include <dali-toolkit/internal/visuals/rendering-addon.h>
41 constexpr auto INITIAL_CACHE_NUMBER = size_t{0u};
42 constexpr auto DEFAULT_NUMBER_OF_LOCAL_LOADER_THREADS = size_t{4u};
43 constexpr auto DEFAULT_NUMBER_OF_REMOTE_LOADER_THREADS = size_t{8u};
45 constexpr auto NUMBER_OF_LOCAL_LOADER_THREADS_ENV = "DALI_TEXTURE_LOCAL_THREADS";
46 constexpr auto NUMBER_OF_REMOTE_LOADER_THREADS_ENV = "DALI_TEXTURE_REMOTE_THREADS";
48 size_t GetNumberOfThreads(const char* environmentVariable, size_t defaultValue)
50 using Dali::EnvironmentVariable::GetEnvironmentVariable;
51 auto numberString = GetEnvironmentVariable(environmentVariable);
52 auto numberOfThreads = numberString ? std::strtoul(numberString, nullptr, 10) : 0;
53 constexpr auto MAX_NUMBER_OF_THREADS = 100u;
54 DALI_ASSERT_DEBUG( numberOfThreads < MAX_NUMBER_OF_THREADS );
55 return ( numberOfThreads > 0 && numberOfThreads < MAX_NUMBER_OF_THREADS ) ? numberOfThreads : defaultValue;
58 size_t GetNumberOfLocalLoaderThreads()
60 return GetNumberOfThreads(NUMBER_OF_LOCAL_LOADER_THREADS_ENV, DEFAULT_NUMBER_OF_LOCAL_LOADER_THREADS);
63 size_t GetNumberOfRemoteLoaderThreads()
65 return GetNumberOfThreads(NUMBER_OF_REMOTE_LOADER_THREADS_ENV, DEFAULT_NUMBER_OF_REMOTE_LOADER_THREADS);
83 Debug::Filter* gTextureManagerLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_TEXTURE_MANAGER" );
85 #define GET_LOAD_STATE_STRING( loadState ) \
86 loadState == TextureManager::NOT_STARTED ? "NOT_STARTED" : \
87 loadState == TextureManager::LOADING ? "LOADING" : \
88 loadState == TextureManager::LOAD_FINISHED ? "LOAD_FINISHED" : \
89 loadState == TextureManager::WAITING_FOR_MASK ? "WAITING_FOR_MASK" : \
90 loadState == TextureManager::MASK_APPLYING ? "MASK_APPLYING" : \
91 loadState == TextureManager::MASK_APPLIED ? "MASK_APPLIED" : \
92 loadState == TextureManager::UPLOADED ? "UPLOADED" : \
93 loadState == TextureManager::CANCELLED ? "CANCELLED" : \
94 loadState == TextureManager::LOAD_FAILED ? "LOAD_FAILED" : "Unknown"
98 const uint32_t DEFAULT_ATLAS_SIZE( 1024u ); ///< This size can fit 8 by 8 images of average size 128 * 128
99 const Vector4 FULL_ATLAS_RECT( 0.0f, 0.0f, 1.0f, 1.0f ); ///< UV Rectangle that covers the full Texture
100 const int INVALID_INDEX( -1 ); ///< Invalid index used to represent a non-existant TextureInfo struct
101 const int INVALID_CACHE_INDEX( -1 ); ///< Invalid Cache index
104 void PreMultiply( Devel::PixelBuffer pixelBuffer, TextureManager::MultiplyOnLoad& preMultiplyOnLoad )
106 if( Pixel::HasAlpha( pixelBuffer.GetPixelFormat() ) )
108 if( preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD )
110 pixelBuffer.MultiplyColorByAlpha();
115 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
119 } // Anonymous namespace
121 TextureManager::MaskingData::MaskingData()
123 mAlphaMaskId( INVALID_TEXTURE_ID ),
124 mContentScaleFactor( 1.0f ),
129 TextureManager::TextureManager()
130 : mAsyncLocalLoaders( GetNumberOfLocalLoaderThreads(), [&]() { return AsyncLoadingHelper(*this); } ),
131 mAsyncRemoteLoaders( GetNumberOfRemoteLoaderThreads(), [&]() { return AsyncLoadingHelper(*this); } ),
133 mLifecycleObservers(),
136 mCurrentTextureId( 0 ),
137 mQueueLoadFlag(false)
139 // Initialize the AddOn
140 RenderingAddOn::Get();
143 TextureManager::~TextureManager()
145 for( auto iter = mLifecycleObservers.Begin(), endIter = mLifecycleObservers.End(); iter != endIter; ++iter)
147 (*iter)->TextureManagerDestroyed();
151 TextureSet TextureManager::LoadAnimatedImageTexture(
152 Dali::AnimatedImageLoading animatedImageLoading, uint32_t frameIndex, Dali::SamplingMode::Type samplingMode,
153 bool synchronousLoading, TextureManager::TextureId& textureId, Dali::WrapMode::Type wrapModeU, Dali::WrapMode::Type wrapModeV, TextureUploadObserver* textureObserver )
155 TextureSet textureSet;
157 if( synchronousLoading )
159 Devel::PixelBuffer pixelBuffer;
160 if( animatedImageLoading )
162 pixelBuffer = animatedImageLoading.LoadFrame( frameIndex );
167 pixelBuffer = LoadImageFromFile( mBrokenImageUrl );
171 pixelData = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
173 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, pixelData.GetPixelFormat(),
174 pixelData.GetWidth(), pixelData.GetHeight() );
175 texture.Upload( pixelData );
176 textureSet = TextureSet::New();
177 textureSet.SetTexture( 0u, texture );
181 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
184 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, pixelData.GetPixelFormat(),
185 pixelData.GetWidth(), pixelData.GetHeight() );
186 texture.Upload( pixelData );
187 textureSet = TextureSet::New();
188 textureSet.SetTexture( 0u, texture );
194 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
195 textureId = RequestLoadInternal( animatedImageLoading.GetUrl(), INVALID_TEXTURE_ID, 1.0f, ImageDimensions(), FittingMode::SCALE_TO_FILL,
196 SamplingMode::BOX_THEN_LINEAR, TextureManager::NO_ATLAS, false, UPLOAD_TO_TEXTURE, textureObserver,
197 true, TextureManager::ReloadPolicy::CACHED, preMultiply, animatedImageLoading, frameIndex );
198 TextureManager::LoadState loadState = GetTextureStateInternal( textureId );
199 if( loadState == TextureManager::UPLOADED )
201 // UploadComplete has already been called - keep the same texture set
202 textureSet = GetTextureSet( textureId );
208 Sampler sampler = Sampler::New();
209 sampler.SetWrapMode( wrapModeU, wrapModeV );
210 textureSet.SetSampler( 0u, sampler );
216 Devel::PixelBuffer TextureManager::LoadPixelBuffer(
217 const VisualUrl& url, Dali::ImageDimensions desiredSize, Dali::FittingMode::Type fittingMode, Dali::SamplingMode::Type samplingMode, bool synchronousLoading, TextureUploadObserver* textureObserver, bool orientationCorrection, TextureManager::MultiplyOnLoad& preMultiplyOnLoad )
219 Devel::PixelBuffer pixelBuffer;
220 if( synchronousLoading )
224 pixelBuffer = LoadImageFromFile( url.GetUrl(), desiredSize, fittingMode, samplingMode,
225 orientationCorrection );
226 if( pixelBuffer && preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD )
228 PreMultiply( pixelBuffer, preMultiplyOnLoad );
234 RequestLoadInternal( url, INVALID_TEXTURE_ID, 1.0f, desiredSize, fittingMode, samplingMode, TextureManager::NO_ATLAS,
235 false, RETURN_PIXEL_BUFFER, textureObserver, orientationCorrection, TextureManager::ReloadPolicy::FORCED,
236 preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u );
242 TextureSet TextureManager::LoadTexture(
243 const VisualUrl& url, Dali::ImageDimensions desiredSize, Dali::FittingMode::Type fittingMode,
244 Dali::SamplingMode::Type samplingMode, MaskingDataPointer& maskInfo,
245 bool synchronousLoading, TextureManager::TextureId& textureId, Vector4& textureRect,
246 Dali::ImageDimensions& textureRectSize, bool& atlasingStatus, bool& loadingStatus,
247 Dali::WrapMode::Type wrapModeU, Dali::WrapMode::Type wrapModeV, TextureUploadObserver* textureObserver,
248 AtlasUploadObserver* atlasObserver, ImageAtlasManagerPtr imageAtlasManager, bool orientationCorrection,
249 TextureManager::ReloadPolicy reloadPolicy, TextureManager::MultiplyOnLoad& preMultiplyOnLoad )
251 TextureSet textureSet;
253 loadingStatus = false;
254 textureRect = FULL_ATLAS_RECT;
256 if( VisualUrl::TEXTURE == url.GetProtocolType())
258 std::string location = url.GetLocation();
259 if( location.size() > 0u )
261 TextureId id = std::stoi( location );
262 for( auto&& elem : mExternalTextures )
264 if( elem.textureId == id )
266 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
267 textureId = elem.textureId;
268 return elem.textureSet;
273 else if( synchronousLoading )
278 Devel::PixelBuffer pixelBuffer = LoadImageFromFile( url.GetUrl(), desiredSize, fittingMode, samplingMode,
279 orientationCorrection );
280 if( maskInfo && maskInfo->mAlphaMaskUrl.IsValid() )
282 Devel::PixelBuffer maskPixelBuffer = LoadImageFromFile( maskInfo->mAlphaMaskUrl.GetUrl(), ImageDimensions(),
283 FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true );
284 if( maskPixelBuffer )
286 pixelBuffer.ApplyMask( maskPixelBuffer, maskInfo->mContentScaleFactor, maskInfo->mCropToMask );
291 PreMultiply( pixelBuffer, preMultiplyOnLoad );
292 data = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
298 Devel::PixelBuffer pixelBuffer = LoadImageFromFile( mBrokenImageUrl );
301 PreMultiply( pixelBuffer, preMultiplyOnLoad );
302 data = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
304 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, data.GetPixelFormat(),
305 data.GetWidth(), data.GetHeight() );
306 texture.Upload( data );
307 textureSet = TextureSet::New();
308 textureSet.SetTexture( 0u, texture );
312 if( atlasingStatus ) // attempt atlasing
314 textureSet = imageAtlasManager->Add( textureRect, data );
316 if( !textureSet ) // big image, no atlasing or atlasing failed
318 atlasingStatus = false;
319 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, data.GetPixelFormat(),
320 data.GetWidth(), data.GetHeight() );
321 texture.Upload( data );
322 textureSet = TextureSet::New();
323 textureSet.SetTexture( 0u, texture );
327 textureRectSize.SetWidth(data.GetWidth());
328 textureRectSize.SetHeight(data.GetHeight());
334 loadingStatus = true;
337 textureSet = imageAtlasManager->Add( textureRect, url.GetUrl(), desiredSize, fittingMode, true, atlasObserver);
339 if( !textureSet ) // big image, no atlasing or atlasing failed
341 atlasingStatus = false;
342 if( !maskInfo || !maskInfo->mAlphaMaskUrl.IsValid() )
344 textureId = RequestLoad( url, desiredSize, fittingMode, samplingMode, TextureManager::NO_ATLAS,
345 textureObserver, orientationCorrection, reloadPolicy, preMultiplyOnLoad );
349 maskInfo->mAlphaMaskId = RequestMaskLoad( maskInfo->mAlphaMaskUrl );
350 textureId = RequestLoad( url,
351 maskInfo->mAlphaMaskId,
352 maskInfo->mContentScaleFactor,
354 fittingMode, samplingMode,
355 TextureManager::NO_ATLAS,
356 maskInfo->mCropToMask,
358 orientationCorrection,
359 reloadPolicy, preMultiplyOnLoad );
362 TextureManager::LoadState loadState = GetTextureStateInternal( textureId );
363 if( loadState == TextureManager::UPLOADED )
365 // UploadComplete has already been called - keep the same texture set
366 textureSet = GetTextureSet( textureId );
369 // If we are loading the texture, or waiting for the ready signal handler to complete, inform
370 // caller that they need to wait.
371 loadingStatus = ( loadState == TextureManager::LOADING ||
372 loadState == TextureManager::WAITING_FOR_MASK ||
373 loadState == TextureManager::MASK_APPLYING ||
374 loadState == TextureManager::MASK_APPLIED ||
375 loadState == TextureManager::NOT_STARTED ||
381 textureRectSize = desiredSize;
385 if( ! atlasingStatus && textureSet )
387 Sampler sampler = Sampler::New();
388 sampler.SetWrapMode( wrapModeU, wrapModeV );
389 textureSet.SetSampler( 0u, sampler );
395 TextureManager::TextureId TextureManager::RequestLoad(
396 const VisualUrl& url,
397 const ImageDimensions desiredSize,
398 FittingMode::Type fittingMode,
399 Dali::SamplingMode::Type samplingMode,
400 const UseAtlas useAtlas,
401 TextureUploadObserver* observer,
402 bool orientationCorrection,
403 TextureManager::ReloadPolicy reloadPolicy,
404 TextureManager::MultiplyOnLoad& preMultiplyOnLoad )
406 return RequestLoadInternal( url, INVALID_TEXTURE_ID, 1.0f, desiredSize, fittingMode, samplingMode, useAtlas,
407 false, UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy,
408 preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u );
411 TextureManager::TextureId TextureManager::RequestLoad(
412 const VisualUrl& url,
413 TextureId maskTextureId,
415 const ImageDimensions desiredSize,
416 FittingMode::Type fittingMode,
417 Dali::SamplingMode::Type samplingMode,
418 const UseAtlas useAtlas,
420 TextureUploadObserver* observer,
421 bool orientationCorrection,
422 TextureManager::ReloadPolicy reloadPolicy,
423 TextureManager::MultiplyOnLoad& preMultiplyOnLoad )
425 return RequestLoadInternal( url, maskTextureId, contentScale, desiredSize, fittingMode, samplingMode, useAtlas,
426 cropToMask, UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy,
427 preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u );
430 TextureManager::TextureId TextureManager::RequestMaskLoad( const VisualUrl& maskUrl )
432 // Use the normal load procedure to get the alpha mask.
433 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
434 return RequestLoadInternal( maskUrl, INVALID_TEXTURE_ID, 1.0f, ImageDimensions(), FittingMode::SCALE_TO_FILL,
435 SamplingMode::NO_FILTER, NO_ATLAS, false, KEEP_PIXEL_BUFFER, NULL, true,
436 TextureManager::ReloadPolicy::CACHED, preMultiply, Dali::AnimatedImageLoading(), 0u );
439 TextureManager::TextureId TextureManager::RequestLoadInternal(
440 const VisualUrl& url,
441 TextureId maskTextureId,
443 const ImageDimensions desiredSize,
444 FittingMode::Type fittingMode,
445 Dali::SamplingMode::Type samplingMode,
448 StorageType storageType,
449 TextureUploadObserver* observer,
450 bool orientationCorrection,
451 TextureManager::ReloadPolicy reloadPolicy,
452 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
453 Dali::AnimatedImageLoading animatedImageLoading,
454 uint32_t frameIndex )
456 // First check if the requested Texture is cached.
457 bool isAnimatedImage = ( animatedImageLoading ) ? true : false;
459 TextureHash textureHash = INITIAL_CACHE_NUMBER;
460 int cacheIndex = INVALID_CACHE_INDEX;
463 textureHash = GenerateHash(url.GetUrl(), desiredSize, fittingMode, samplingMode, useAtlas, maskTextureId, storageType);
465 // Look up the texture by hash. Note: The extra parameters are used in case of a hash collision.
466 cacheIndex = FindCachedTexture(textureHash, url.GetUrl(), desiredSize, fittingMode, samplingMode, useAtlas, maskTextureId, preMultiplyOnLoad, storageType);
469 TextureManager::TextureId textureId = INVALID_TEXTURE_ID;
470 // Check if the requested Texture exists in the cache.
471 if( cacheIndex != INVALID_CACHE_INDEX )
473 if ( TextureManager::ReloadPolicy::CACHED == reloadPolicy )
475 // Mark this texture being used by another client resource. Forced reload would replace the current texture
476 // without the need for incrementing the reference count.
477 ++( mTextureInfoContainer[ cacheIndex ].referenceCount );
479 textureId = mTextureInfoContainer[ cacheIndex ].textureId;
481 // Update preMultiplyOnLoad value. It should be changed according to preMultiplied value of the cached info.
482 preMultiplyOnLoad = mTextureInfoContainer[ cacheIndex ].preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
484 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::General, "TextureManager::RequestLoad( url=%s observer=%p ) Using cached texture id@%d, textureId=%d\n",
485 url.GetUrl().c_str(), observer, cacheIndex, textureId );
488 if( textureId == INVALID_TEXTURE_ID ) // There was no caching, or caching not required
490 // We need a new Texture.
491 textureId = GenerateUniqueTextureId();
492 bool preMultiply = ( preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD );
493 mTextureInfoContainer.push_back( TextureInfo( textureId, maskTextureId, url.GetUrl(),
494 desiredSize, contentScale, fittingMode, samplingMode,
495 false, cropToMask, useAtlas, textureHash, orientationCorrection,
496 preMultiply, animatedImageLoading, frameIndex ) );
497 cacheIndex = mTextureInfoContainer.size() - 1u;
499 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::General, "TextureManager::RequestLoad( url=%s observer=%p ) New texture, cacheIndex:%d, textureId=%d\n",
500 url.GetUrl().c_str(), observer, cacheIndex, textureId );
503 // The below code path is common whether we are using the cache or not.
504 // The textureInfoIndex now refers to either a pre-existing cached TextureInfo,
505 // or a new TextureInfo just created.
506 TextureInfo& textureInfo( mTextureInfoContainer[ cacheIndex ] );
507 textureInfo.maskTextureId = maskTextureId;
508 textureInfo.storageType = storageType;
509 textureInfo.orientationCorrection = orientationCorrection;
511 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::General, "TextureInfo loadState:%s\n",
512 GET_LOAD_STATE_STRING(textureInfo.loadState ) );
514 // Force reloading of texture by setting loadState unless already loading or cancelled.
515 if ( TextureManager::ReloadPolicy::FORCED == reloadPolicy &&
516 TextureManager::LOADING != textureInfo.loadState &&
517 TextureManager::WAITING_FOR_MASK != textureInfo.loadState &&
518 TextureManager::MASK_APPLYING != textureInfo.loadState &&
519 TextureManager::MASK_APPLIED != textureInfo.loadState &&
520 TextureManager::CANCELLED != textureInfo.loadState )
522 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Verbose, "TextureManager::RequestLoad( url=%s observer=%p ) ForcedReload cacheIndex:%d, textureId=%d\n",
523 url.GetUrl().c_str(), observer, cacheIndex, textureId );
525 textureInfo.loadState = TextureManager::NOT_STARTED;
528 // Check if we should add the observer.
529 // Only do this if we have not loaded yet and it will not have loaded by the end of this method.
530 switch( textureInfo.loadState )
532 case TextureManager::LOAD_FAILED: // Failed notifies observer which then stops observing.
533 case TextureManager::NOT_STARTED:
535 LoadOrQueueTexture( textureInfo, observer ); // If called inside NotifyObservers, queues until afterwards
538 case TextureManager::LOADING:
539 case TextureManager::WAITING_FOR_MASK:
540 case TextureManager::MASK_APPLYING:
541 case TextureManager::MASK_APPLIED:
543 ObserveTexture( textureInfo, observer );
546 case TextureManager::UPLOADED:
550 LoadOrQueueTexture( textureInfo, observer );
554 case TextureManager::CANCELLED:
556 // A cancelled texture hasn't finished loading yet. Treat as a loading texture
557 // (it's ref count has already been incremented, above)
558 textureInfo.loadState = TextureManager::LOADING;
559 ObserveTexture( textureInfo, observer );
562 case TextureManager::LOAD_FINISHED:
564 // Loading has already completed.
565 if( observer && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER )
567 LoadOrQueueTexture( textureInfo, observer );
573 // Return the TextureId for which this Texture can now be referenced by externally.
577 void TextureManager::Remove( const TextureManager::TextureId textureId, TextureUploadObserver* observer )
579 int textureInfoIndex = GetCacheIndexFromId( textureId );
580 if( textureInfoIndex != INVALID_INDEX )
582 TextureInfo& textureInfo( mTextureInfoContainer[ textureInfoIndex ] );
584 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise,
585 "TextureManager::Remove(%d) url:%s\n cacheIdx:%d loadState:%s reference count = %d\n",
586 textureId, textureInfo.url.GetUrl().c_str(),
587 textureInfoIndex, GET_LOAD_STATE_STRING( textureInfo.loadState ), textureInfo.referenceCount );
589 // Decrement the reference count and check if this is the last user of this Texture.
590 if( --textureInfo.referenceCount <= 0 )
592 // This is the last remove for this Texture.
593 textureInfo.referenceCount = 0;
594 bool removeTextureInfo = false;
596 // If loaded, we can remove the TextureInfo and the Atlas (if atlased).
597 if( textureInfo.loadState == UPLOADED )
599 if( textureInfo.atlas )
601 textureInfo.atlas.Remove( textureInfo.atlasRect );
603 removeTextureInfo = true;
605 else if( textureInfo.loadState == LOADING )
607 // We mark the textureInfo for removal.
608 // Once the load has completed, this method will be called again.
609 textureInfo.loadState = CANCELLED;
613 // In other states, we are not waiting for a load so we are safe to remove the TextureInfo data.
614 removeTextureInfo = true;
617 // If the state allows us to remove the TextureInfo data, we do so.
618 if( removeTextureInfo )
620 // Permanently remove the textureInfo struct.
621 mTextureInfoContainer.erase( mTextureInfoContainer.begin() + textureInfoIndex );
627 // Remove element from the LoadQueue
628 for( auto&& element : mLoadQueue )
630 if( element.mObserver == observer )
632 // Do not erase the item. We will clear it later in ProcessQueuedTextures().
633 element.mObserver = nullptr;
641 VisualUrl TextureManager::GetVisualUrl( TextureId textureId )
643 VisualUrl visualUrl("");
644 int cacheIndex = GetCacheIndexFromId( textureId );
646 if( cacheIndex != INVALID_CACHE_INDEX )
648 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::GetVisualUrl. Using cached texture id=%d, textureId=%d\n",
649 cacheIndex, textureId );
651 TextureInfo& cachedTextureInfo( mTextureInfoContainer[ cacheIndex ] );
652 visualUrl = cachedTextureInfo.url;
657 TextureManager::LoadState TextureManager::GetTextureState( TextureId textureId )
659 LoadState loadState = TextureManager::NOT_STARTED;
661 int cacheIndex = GetCacheIndexFromId( textureId );
662 if( cacheIndex != INVALID_CACHE_INDEX )
664 TextureInfo& cachedTextureInfo( mTextureInfoContainer[ cacheIndex ] );
665 loadState = cachedTextureInfo.loadState;
669 for( auto&& elem : mExternalTextures )
671 if( elem.textureId == textureId )
673 loadState = LoadState::UPLOADED;
681 TextureManager::LoadState TextureManager::GetTextureStateInternal( TextureId textureId )
683 LoadState loadState = TextureManager::NOT_STARTED;
685 int cacheIndex = GetCacheIndexFromId( textureId );
686 if( cacheIndex != INVALID_CACHE_INDEX )
688 TextureInfo& cachedTextureInfo( mTextureInfoContainer[ cacheIndex ] );
689 loadState = cachedTextureInfo.loadState;
695 TextureSet TextureManager::GetTextureSet( TextureId textureId )
697 TextureSet textureSet;// empty handle
699 int cacheIndex = GetCacheIndexFromId( textureId );
700 if( cacheIndex != INVALID_CACHE_INDEX )
702 TextureInfo& cachedTextureInfo( mTextureInfoContainer[ cacheIndex ] );
703 textureSet = cachedTextureInfo.textureSet;
707 for( auto&& elem : mExternalTextures )
709 if( elem.textureId == textureId )
711 textureSet = elem.textureSet;
719 std::string TextureManager::AddExternalTexture( TextureSet& textureSet )
721 TextureManager::ExternalTextureInfo info;
722 info.textureId = GenerateUniqueTextureId();
723 info.textureSet = textureSet;
724 mExternalTextures.emplace_back( info );
725 return VisualUrl::CreateTextureUrl( std::to_string( info.textureId ) );
728 TextureSet TextureManager::RemoveExternalTexture( const std::string& url )
730 if( url.size() > 0u )
732 // get the location from the Url
733 VisualUrl parseUrl( url );
734 if( VisualUrl::TEXTURE == parseUrl.GetProtocolType() )
736 std::string location = parseUrl.GetLocation();
737 if( location.size() > 0u )
739 TextureId id = std::stoi( location );
740 const auto end = mExternalTextures.end();
741 for( auto iter = mExternalTextures.begin(); iter != end; ++iter )
743 if( iter->textureId == id )
745 auto textureSet = iter->textureSet;
746 mExternalTextures.erase( iter );
756 void TextureManager::AddObserver( TextureManager::LifecycleObserver& observer )
758 // make sure an observer doesn't observe the same object twice
759 // otherwise it will get multiple calls to ObjectDestroyed()
760 DALI_ASSERT_DEBUG( mLifecycleObservers.End() == std::find( mLifecycleObservers.Begin(), mLifecycleObservers.End(), &observer));
761 mLifecycleObservers.PushBack( &observer );
764 void TextureManager::RemoveObserver( TextureManager::LifecycleObserver& observer)
766 // Find the observer...
767 auto endIter = mLifecycleObservers.End();
768 for( auto iter = mLifecycleObservers.Begin(); iter != endIter; ++iter)
770 if( (*iter) == &observer)
772 mLifecycleObservers.Erase( iter );
776 DALI_ASSERT_DEBUG(endIter != mLifecycleObservers.End());
779 void TextureManager::LoadOrQueueTexture( TextureInfo& textureInfo, TextureUploadObserver* observer )
781 switch( textureInfo.loadState )
788 QueueLoadTexture( textureInfo, observer );
792 LoadTexture( textureInfo, observer );
800 QueueLoadTexture( textureInfo, observer );
804 // The Texture has already loaded. The other observers have already been notified.
805 // We need to send a "late" loaded notification for this observer.
806 observer->UploadComplete( true, textureInfo.textureId, textureInfo.textureSet,
807 textureInfo.useAtlas, textureInfo.atlasRect,
808 textureInfo.preMultiplied );
815 case WAITING_FOR_MASK:
824 void TextureManager::QueueLoadTexture( TextureInfo& textureInfo, TextureUploadObserver* observer )
826 auto textureId = textureInfo.textureId;
827 mLoadQueue.PushBack( LoadQueueElement( textureId, observer) );
829 observer->DestructionSignal().Connect( this, &TextureManager::ObserverDestroyed );
832 void TextureManager::LoadTexture( TextureInfo& textureInfo, TextureUploadObserver* observer )
834 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::LoadTexture(): url:%s sync:%s\n",
835 textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously?"T":"F" );
837 textureInfo.loadState = LOADING;
838 if( !textureInfo.loadSynchronously )
840 auto& loadersContainer = textureInfo.url.IsLocalResource() ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
841 auto loadingHelperIt = loadersContainer.GetNext();
842 auto premultiplyOnLoad = ( textureInfo.preMultiplyOnLoad && textureInfo.maskTextureId == INVALID_TEXTURE_ID ) ?
843 DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
844 DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
845 if( textureInfo.animatedImageLoading )
847 loadingHelperIt->LoadAnimatedImage( textureInfo.textureId, textureInfo.animatedImageLoading, textureInfo.frameIndex );
851 loadingHelperIt->Load(textureInfo.textureId, textureInfo.url,
852 textureInfo.desiredSize, textureInfo.fittingMode,
853 textureInfo.samplingMode, textureInfo.orientationCorrection,
857 ObserveTexture( textureInfo, observer );
860 void TextureManager::ProcessQueuedTextures()
862 for( auto&& element : mLoadQueue )
864 if( !element.mObserver )
869 int cacheIndex = GetCacheIndexFromId( element.mTextureId );
870 if( cacheIndex != INVALID_CACHE_INDEX )
872 TextureInfo& textureInfo( mTextureInfoContainer[cacheIndex] );
873 if( textureInfo.loadState == UPLOADED )
875 element.mObserver->UploadComplete( true, textureInfo.textureId, textureInfo.textureSet,
876 textureInfo.useAtlas, textureInfo.atlasRect,
877 textureInfo.preMultiplied );
879 else if ( textureInfo.loadState == LOAD_FINISHED && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER )
881 element.mObserver->LoadComplete( true, textureInfo.pixelBuffer, textureInfo.url, textureInfo.preMultiplied );
883 else if(textureInfo.loadState == LOADING)
885 // Note : LOADING state texture cannot be queue.
886 // This case be occured when same texture id are queue in mLoadQueue.
887 ObserveTexture( textureInfo, element.mObserver );
891 LoadTexture( textureInfo, element.mObserver );
898 void TextureManager::ObserveTexture( TextureInfo& textureInfo,
899 TextureUploadObserver* observer )
901 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::ObserveTexture(): url:%s observer:%p\n",
902 textureInfo.url.GetUrl().c_str(), observer );
906 textureInfo.observerList.PushBack( observer );
907 observer->DestructionSignal().Connect( this, &TextureManager::ObserverDestroyed );
911 void TextureManager::AsyncLoadComplete( AsyncLoadingInfoContainerType& loadingContainer, uint32_t id,
912 Devel::PixelBuffer pixelBuffer )
914 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::AsyncLoadComplete( id:%d )\n", id );
916 if( loadingContainer.size() >= 1u )
918 AsyncLoadingInfo loadingInfo = loadingContainer.front();
920 if( loadingInfo.loadId == id )
922 int cacheIndex = GetCacheIndexFromId( loadingInfo.textureId );
923 if( cacheIndex != INVALID_CACHE_INDEX )
925 TextureInfo& textureInfo( mTextureInfoContainer[cacheIndex] );
927 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise,
928 " textureId:%d Url:%s CacheIndex:%d LoadState: %d\n",
929 textureInfo.textureId, textureInfo.url.GetUrl().c_str(), cacheIndex, textureInfo.loadState );
931 if( textureInfo.loadState != CANCELLED )
933 // textureInfo can be invalidated after this call (as the mTextureInfoContainer may be modified)
934 PostLoad( textureInfo, pixelBuffer );
938 Remove( textureInfo.textureId, nullptr );
943 loadingContainer.pop_front();
947 void TextureManager::PostLoad( TextureInfo& textureInfo, Devel::PixelBuffer& pixelBuffer )
949 // Was the load successful?
950 if( pixelBuffer && ( pixelBuffer.GetWidth() != 0 ) && ( pixelBuffer.GetHeight() != 0 ) )
952 // No atlas support for now
953 textureInfo.useAtlas = NO_ATLAS;
954 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
956 if( textureInfo.storageType == UPLOAD_TO_TEXTURE )
958 // If there is a mask texture ID associated with this texture, then apply the mask
959 // if it's already loaded. If it hasn't, and the mask is still loading,
960 // wait for the mask to finish loading.
961 if( textureInfo.maskTextureId != INVALID_TEXTURE_ID )
963 if( textureInfo.loadState == MASK_APPLYING )
965 textureInfo.loadState = MASK_APPLIED;
966 UploadTexture( pixelBuffer, textureInfo );
967 NotifyObservers( textureInfo, true );
971 LoadState maskLoadState = GetTextureStateInternal( textureInfo.maskTextureId );
972 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
973 if( maskLoadState == LOADING )
975 textureInfo.loadState = WAITING_FOR_MASK;
977 else if( maskLoadState == LOAD_FINISHED )
979 // Send New Task to Thread
980 ApplyMask( textureInfo, textureInfo.maskTextureId );
986 UploadTexture( pixelBuffer, textureInfo );
987 NotifyObservers( textureInfo, true );
992 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
993 textureInfo.loadState = LOAD_FINISHED;
995 if( textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER )
997 NotifyObservers( textureInfo, true );
1001 // Check if there was another texture waiting for this load to complete
1002 // (e.g. if this was an image mask, and its load is on a different thread)
1003 CheckForWaitingTexture( textureInfo );
1009 // @todo If the load was unsuccessful, upload the broken image.
1010 textureInfo.loadState = LOAD_FAILED;
1011 if(textureInfo.storageType == StorageType::KEEP_PIXEL_BUFFER)
1013 // Check if there was another texture waiting for this load to complete
1014 // (e.g. if this was an image mask, and its load is on a different thread)
1015 CheckForWaitingTexture(textureInfo);
1019 NotifyObservers(textureInfo, false);
1024 void TextureManager::CheckForWaitingTexture( TextureInfo& maskTextureInfo )
1026 // Search the cache, checking if any texture has this texture id as a
1028 const unsigned int size = mTextureInfoContainer.size();
1030 // Keep notify observer required textureIds.
1031 // Note : NotifyObservers can change mTextureInfoContainer cache struct. We should check id's validation before notify.
1032 std::vector<TextureId> notifyRequiredTextureIds;
1034 for( unsigned int cacheIndex = 0; cacheIndex < size; ++cacheIndex )
1036 if( mTextureInfoContainer[cacheIndex].maskTextureId == maskTextureInfo.textureId &&
1037 mTextureInfoContainer[cacheIndex].loadState == WAITING_FOR_MASK )
1039 TextureInfo& textureInfo( mTextureInfoContainer[cacheIndex] );
1041 if( maskTextureInfo.loadState == LOAD_FINISHED )
1043 // Send New Task to Thread
1044 ApplyMask( textureInfo, maskTextureInfo.textureId );
1046 else // maskTextureInfo.loadState == LoadState::LOAD_FAILED
1048 // Increase reference counts for notify required textureId.
1049 // Now we can assume that we don't remove & re-assign this textureId
1050 // during NotifyObserver signal emit.
1051 textureInfo.referenceCount++;
1053 textureInfo.pixelBuffer.Reset();
1054 textureInfo.loadState = LOAD_FAILED;
1056 notifyRequiredTextureIds.push_back(textureInfo.textureId);
1061 // Notify textures are masked
1062 for(const auto textureId : notifyRequiredTextureIds)
1064 int textureCacheIndex = GetCacheIndexFromId(textureId);
1065 if(textureCacheIndex != INVALID_CACHE_INDEX)
1067 TextureInfo& textureInfo(mTextureInfoContainer[textureCacheIndex]);
1068 NotifyObservers(textureInfo, false);
1072 // Decrease reference count
1073 for(const auto textureId : notifyRequiredTextureIds)
1075 Remove(textureId, nullptr);
1079 void TextureManager::ApplyMask( TextureInfo& textureInfo, TextureId maskTextureId )
1081 int maskCacheIndex = GetCacheIndexFromId( maskTextureId );
1082 if( maskCacheIndex != INVALID_CACHE_INDEX )
1084 Devel::PixelBuffer maskPixelBuffer = mTextureInfoContainer[maskCacheIndex].pixelBuffer;
1085 Devel::PixelBuffer pixelBuffer = textureInfo.pixelBuffer;
1086 textureInfo.pixelBuffer.Reset();
1088 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::ApplyMask(): url:%s sync:%s\n",
1089 textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously?"T":"F" );
1091 textureInfo.loadState = MASK_APPLYING;
1092 auto& loadersContainer = textureInfo.url.IsLocalResource() ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
1093 auto loadingHelperIt = loadersContainer.GetNext();
1094 auto premultiplyOnLoad = textureInfo.preMultiplyOnLoad ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
1095 DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
1096 loadingHelperIt->ApplyMask( textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask, premultiplyOnLoad );
1100 void TextureManager::UploadTexture( Devel::PixelBuffer& pixelBuffer, TextureInfo& textureInfo )
1102 if( textureInfo.useAtlas != USE_ATLAS )
1104 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::General, " TextureManager::UploadTexture() New Texture for textureId:%d\n", textureInfo.textureId );
1106 // Check if this pixelBuffer is premultiplied
1107 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
1109 auto& renderingAddOn = RenderingAddOn::Get();
1110 if( renderingAddOn.IsValid() )
1112 renderingAddOn.CreateGeometry( textureInfo.textureId, pixelBuffer );
1115 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(),
1116 pixelBuffer.GetWidth(), pixelBuffer.GetHeight() );
1118 PixelData pixelData = Devel::PixelBuffer::Convert( pixelBuffer );
1119 texture.Upload( pixelData );
1120 if ( ! textureInfo.textureSet )
1122 textureInfo.textureSet = TextureSet::New();
1124 textureInfo.textureSet.SetTexture( 0u, texture );
1127 // Update the load state.
1128 // Note: This is regardless of success as we care about whether a
1129 // load attempt is in progress or not. If unsuccessful, a broken
1130 // image is still loaded.
1131 textureInfo.loadState = UPLOADED;
1134 void TextureManager::NotifyObservers( TextureInfo& textureInfo, bool success )
1136 TextureId textureId = textureInfo.textureId;
1138 // If there is an observer: Notify the load is complete, whether successful or not,
1139 // and erase it from the list
1140 TextureInfo* info = &textureInfo;
1142 mQueueLoadFlag = true;
1144 while( info->observerList.Count() )
1146 TextureUploadObserver* observer = info->observerList[0];
1148 // During UploadComplete() a Control ResourceReady() signal is emitted.
1149 // During that signal the app may add remove /add Textures (e.g. via
1151 // It is possible for observers to be removed from the observer list,
1152 // and it is also possible for the mTextureInfoContainer to be modified,
1153 // invalidating the reference to the textureInfo struct.
1154 // Texture load requests for the same URL are deferred until the end of this
1156 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "NotifyObservers() url:%s loadState:%s\n",
1157 textureInfo.url.GetUrl().c_str(), GET_LOAD_STATE_STRING(textureInfo.loadState ) );
1159 // It is possible for the observer to be deleted.
1160 // Disconnect and remove the observer first.
1161 observer->DestructionSignal().Disconnect( this, &TextureManager::ObserverDestroyed );
1163 info->observerList.Erase( info->observerList.begin() );
1165 if( info->storageType == StorageType::RETURN_PIXEL_BUFFER )
1167 observer->LoadComplete( success, info->pixelBuffer, info->url, info->preMultiplied );
1171 observer->UploadComplete( success, info->textureId, info->textureSet, info->useAtlas, info->atlasRect,
1172 info->preMultiplied );
1175 // Get the textureInfo from the container again as it may have been invalidated.
1176 int textureInfoIndex = GetCacheIndexFromId( textureId );
1177 if( textureInfoIndex == INVALID_CACHE_INDEX)
1179 break; // texture has been removed - can stop.
1181 info = &mTextureInfoContainer[ textureInfoIndex ];
1184 mQueueLoadFlag = false;
1185 ProcessQueuedTextures();
1187 if( info->storageType == StorageType::RETURN_PIXEL_BUFFER && info->observerList.Count() == 0 )
1189 Remove( info->textureId, nullptr );
1193 TextureManager::TextureId TextureManager::GenerateUniqueTextureId()
1195 return mCurrentTextureId++;
1198 int TextureManager::GetCacheIndexFromId( const TextureId textureId )
1200 const unsigned int size = mTextureInfoContainer.size();
1202 for( unsigned int i = 0; i < size; ++i )
1204 if( mTextureInfoContainer[i].textureId == textureId )
1210 return INVALID_CACHE_INDEX;
1213 TextureManager::TextureHash TextureManager::GenerateHash(
1214 const std::string& url,
1215 const ImageDimensions size,
1216 const FittingMode::Type fittingMode,
1217 const Dali::SamplingMode::Type samplingMode,
1218 const UseAtlas useAtlas,
1219 TextureId maskTextureId,
1220 StorageType storageType)
1222 std::string hashTarget( url );
1223 const size_t urlLength = hashTarget.length();
1224 const uint16_t width = size.GetWidth();
1225 const uint16_t height = size.GetWidth();
1227 // If either the width or height has been specified, include the resizing options in the hash
1228 if( width != 0 || height != 0 )
1230 // We are appending 5 bytes to the URL to form the hash input.
1231 hashTarget.resize( urlLength + 5u );
1232 char* hashTargetPtr = &( hashTarget[ urlLength ] );
1234 // Pack the width and height (4 bytes total).
1235 *hashTargetPtr++ = size.GetWidth() & 0xff;
1236 *hashTargetPtr++ = ( size.GetWidth() >> 8u ) & 0xff;
1237 *hashTargetPtr++ = size.GetHeight() & 0xff;
1238 *hashTargetPtr++ = ( size.GetHeight() >> 8u ) & 0xff;
1240 // Bit-pack the FittingMode, SamplingMode and atlasing.
1241 // FittingMode=2bits, SamplingMode=3bits, useAtlas=1bit, storageType=2bits
1242 *hashTargetPtr = ( fittingMode << 6u ) | ( samplingMode << 3 ) | ( useAtlas << 2 ) | storageType;
1246 // We are not including sizing information, but we still need an extra byte for atlasing.
1247 hashTarget.resize( urlLength + 1u );
1249 // Add the atlasing to the hash input.
1252 case UseAtlas::NO_ATLAS:
1254 hashTarget[ urlLength ] = 'f';
1257 case UseAtlas::USE_ATLAS:
1259 hashTarget[ urlLength ] = 't';
1265 if( maskTextureId != INVALID_TEXTURE_ID )
1267 auto textureIdIndex = hashTarget.length();
1268 hashTarget.resize( hashTarget.length() + sizeof( TextureId ) );
1269 unsigned char* hashTargetPtr = reinterpret_cast<unsigned char*>(&( hashTarget[ textureIdIndex ] ));
1271 // Append the texture id to the end of the URL byte by byte:
1272 // (to avoid SIGBUS / alignment issues)
1273 for( size_t byteIter = 0; byteIter < sizeof( TextureId ); ++byteIter )
1275 *hashTargetPtr++ = maskTextureId & 0xff;
1276 maskTextureId >>= 8u;
1280 return Dali::CalculateHash( hashTarget );
1283 int TextureManager::FindCachedTexture(
1284 const TextureManager::TextureHash hash,
1285 const std::string& url,
1286 const ImageDimensions size,
1287 const FittingMode::Type fittingMode,
1288 const Dali::SamplingMode::Type samplingMode,
1289 const bool useAtlas,
1290 TextureId maskTextureId,
1291 TextureManager::MultiplyOnLoad preMultiplyOnLoad,
1292 StorageType storageType)
1294 // Default to an invalid ID, in case we do not find a match.
1295 int cacheIndex = INVALID_CACHE_INDEX;
1297 // Iterate through our hashes to find a match.
1298 const unsigned int count = mTextureInfoContainer.size();
1299 for( unsigned int i = 0u; i < count; ++i )
1301 if( mTextureInfoContainer[i].hash == hash )
1303 // We have a match, now we check all the original parameters in case of a hash collision.
1304 TextureInfo& textureInfo( mTextureInfoContainer[i] );
1306 if( ( url == textureInfo.url.GetUrl() ) &&
1307 ( useAtlas == textureInfo.useAtlas ) &&
1308 ( maskTextureId == textureInfo.maskTextureId ) &&
1309 ( size == textureInfo.desiredSize ) &&
1310 ( ( size.GetWidth() == 0 && size.GetHeight() == 0 ) ||
1311 ( fittingMode == textureInfo.fittingMode &&
1312 samplingMode == textureInfo.samplingMode ) ) &&
1313 ( storageType == textureInfo.storageType ) )
1315 // 1. If preMultiplyOnLoad is MULTIPLY_ON_LOAD, then textureInfo.preMultiplyOnLoad should be true. The premultiplication result can be different.
1316 // 2. If preMultiplyOnLoad is LOAD_WITHOUT_MULTIPLY, then textureInfo.preMultiplied should be false.
1317 if( ( preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD && textureInfo.preMultiplyOnLoad )
1318 || ( preMultiplyOnLoad == TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY && !textureInfo.preMultiplied ) )
1320 // The found Texture is a match.
1331 void TextureManager::ObserverDestroyed( TextureUploadObserver* observer )
1333 const unsigned int count = mTextureInfoContainer.size();
1334 for( unsigned int i = 0; i < count; ++i )
1336 TextureInfo& textureInfo( mTextureInfoContainer[i] );
1337 for( TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin();
1338 j != textureInfo.observerList.End(); )
1340 if( *j == observer )
1342 j = textureInfo.observerList.Erase( j );
1351 // Remove element from the LoadQueue
1352 for( auto&& element : mLoadQueue )
1354 if( element.mObserver == observer )
1356 element.mObserver = nullptr;
1362 TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(TextureManager& textureManager)
1363 : AsyncLoadingHelper(Toolkit::AsyncImageLoader::New(), textureManager,
1364 AsyncLoadingInfoContainerType())
1368 void TextureManager::AsyncLoadingHelper::LoadAnimatedImage( TextureId textureId,
1369 Dali::AnimatedImageLoading animatedImageLoading,
1370 uint32_t frameIndex )
1372 mLoadingInfoContainer.push_back( AsyncLoadingInfo( textureId ) );
1373 auto id = DevelAsyncImageLoader::LoadAnimatedImage( mLoader, animatedImageLoading, frameIndex );
1374 mLoadingInfoContainer.back().loadId = id;
1377 void TextureManager::AsyncLoadingHelper::Load( TextureId textureId,
1378 const VisualUrl& url,
1379 ImageDimensions desiredSize,
1380 FittingMode::Type fittingMode,
1381 SamplingMode::Type samplingMode,
1382 bool orientationCorrection,
1383 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad )
1385 mLoadingInfoContainer.push_back( AsyncLoadingInfo( textureId ) );
1386 auto id = DevelAsyncImageLoader::Load( mLoader, url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad );
1387 mLoadingInfoContainer.back().loadId = id;
1390 void TextureManager::AsyncLoadingHelper::ApplyMask( TextureId textureId,
1391 Devel::PixelBuffer pixelBuffer,
1392 Devel::PixelBuffer maskPixelBuffer,
1395 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad )
1397 mLoadingInfoContainer.push_back( AsyncLoadingInfo( textureId ) );
1398 auto id = DevelAsyncImageLoader::ApplyMask( mLoader, pixelBuffer, maskPixelBuffer, contentScale, cropToMask, preMultiplyOnLoad );
1399 mLoadingInfoContainer.back().loadId = id;
1402 TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(AsyncLoadingHelper&& rhs)
1403 : AsyncLoadingHelper(rhs.mLoader, rhs.mTextureManager, std::move(rhs.mLoadingInfoContainer))
1407 TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(
1408 Toolkit::AsyncImageLoader loader,
1409 TextureManager& textureManager,
1410 AsyncLoadingInfoContainerType&& loadingInfoContainer)
1412 mTextureManager(textureManager),
1413 mLoadingInfoContainer(std::move(loadingInfoContainer))
1415 DevelAsyncImageLoader::PixelBufferLoadedSignal(mLoader).Connect(
1416 this, &AsyncLoadingHelper::AsyncLoadComplete);
1419 void TextureManager::AsyncLoadingHelper::AsyncLoadComplete(uint32_t id,
1420 Devel::PixelBuffer pixelBuffer )
1422 mTextureManager.AsyncLoadComplete( mLoadingInfoContainer, id, pixelBuffer );
1425 void TextureManager::SetBrokenImageUrl(const std::string& brokenImageUrl)
1427 mBrokenImageUrl = brokenImageUrl;
1430 Geometry TextureManager::GetRenderGeometry(TextureId textureId, uint32_t& frontElements, uint32_t& backElements )
1432 return RenderingAddOn::Get().IsValid() ?
1433 RenderingAddOn::Get().GetGeometry( textureId, frontElements, backElements) :
1437 } // namespace Internal
1439 } // namespace Toolkit