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 DEFAULT_NUMBER_OF_LOCAL_LOADER_THREADS = size_t{4u};
42 constexpr auto DEFAULT_NUMBER_OF_REMOTE_LOADER_THREADS = size_t{8u};
44 constexpr auto NUMBER_OF_LOCAL_LOADER_THREADS_ENV = "DALI_TEXTURE_LOCAL_THREADS";
45 constexpr auto NUMBER_OF_REMOTE_LOADER_THREADS_ENV = "DALI_TEXTURE_REMOTE_THREADS";
47 size_t GetNumberOfThreads(const char* environmentVariable, size_t defaultValue)
49 using Dali::EnvironmentVariable::GetEnvironmentVariable;
50 auto numberString = GetEnvironmentVariable(environmentVariable);
51 auto numberOfThreads = numberString ? std::strtoul(numberString, nullptr, 10) : 0;
52 constexpr auto MAX_NUMBER_OF_THREADS = 100u;
53 DALI_ASSERT_DEBUG( numberOfThreads < MAX_NUMBER_OF_THREADS );
54 return ( numberOfThreads > 0 && numberOfThreads < MAX_NUMBER_OF_THREADS ) ? numberOfThreads : defaultValue;
57 size_t GetNumberOfLocalLoaderThreads()
59 return GetNumberOfThreads(NUMBER_OF_LOCAL_LOADER_THREADS_ENV, DEFAULT_NUMBER_OF_LOCAL_LOADER_THREADS);
62 size_t GetNumberOfRemoteLoaderThreads()
64 return GetNumberOfThreads(NUMBER_OF_REMOTE_LOADER_THREADS_ENV, DEFAULT_NUMBER_OF_REMOTE_LOADER_THREADS);
82 Debug::Filter* gTextureManagerLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_TEXTURE_MANAGER" );
84 #define GET_LOAD_STATE_STRING( loadState ) \
85 loadState == TextureManager::NOT_STARTED ? "NOT_STARTED" : \
86 loadState == TextureManager::LOADING ? "LOADING" : \
87 loadState == TextureManager::LOAD_FINISHED ? "LOAD_FINISHED" : \
88 loadState == TextureManager::WAITING_FOR_MASK ? "WAITING_FOR_MASK" : \
89 loadState == TextureManager::MASK_APPLYING ? "MASK_APPLYING" : \
90 loadState == TextureManager::MASK_APPLIED ? "MASK_APPLIED" : \
91 loadState == TextureManager::UPLOADED ? "UPLOADED" : \
92 loadState == TextureManager::CANCELLED ? "CANCELLED" : \
93 loadState == TextureManager::LOAD_FAILED ? "LOAD_FAILED" : "Unknown"
97 const uint32_t DEFAULT_ATLAS_SIZE( 1024u ); ///< This size can fit 8 by 8 images of average size 128 * 128
98 const Vector4 FULL_ATLAS_RECT( 0.0f, 0.0f, 1.0f, 1.0f ); ///< UV Rectangle that covers the full Texture
99 const int INVALID_INDEX( -1 ); ///< Invalid index used to represent a non-existant TextureInfo struct
100 const int INVALID_CACHE_INDEX( -1 ); ///< Invalid Cache index
103 void PreMultiply( Devel::PixelBuffer pixelBuffer, TextureManager::MultiplyOnLoad& preMultiplyOnLoad )
105 if( Pixel::HasAlpha( pixelBuffer.GetPixelFormat() ) )
107 if( preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD )
109 pixelBuffer.MultiplyColorByAlpha();
114 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
118 } // Anonymous namespace
120 TextureManager::MaskingData::MaskingData()
122 mAlphaMaskId( INVALID_TEXTURE_ID ),
123 mContentScaleFactor( 1.0f ),
128 TextureManager::TextureManager()
129 : mAsyncLocalLoaders( GetNumberOfLocalLoaderThreads(), [&]() { return AsyncLoadingHelper(*this); } ),
130 mAsyncRemoteLoaders( GetNumberOfRemoteLoaderThreads(), [&]() { return AsyncLoadingHelper(*this); } ),
132 mLifecycleObservers(),
135 mCurrentTextureId( 0 ),
136 mQueueLoadFlag(false)
138 // Initialize the AddOn
139 RenderingAddOn::Get();
142 TextureManager::~TextureManager()
144 for( auto iter = mLifecycleObservers.Begin(), endIter = mLifecycleObservers.End(); iter != endIter; ++iter)
146 (*iter)->TextureManagerDestroyed();
150 TextureSet TextureManager::LoadAnimatedImageTexture(
151 Dali::AnimatedImageLoading animatedImageLoading, uint32_t frameIndex, Dali::SamplingMode::Type samplingMode,
152 bool synchronousLoading, TextureManager::TextureId& textureId, Dali::WrapMode::Type wrapModeU, Dali::WrapMode::Type wrapModeV, TextureUploadObserver* textureObserver )
154 TextureSet textureSet;
156 if( synchronousLoading )
158 Devel::PixelBuffer pixelBuffer;
159 if( animatedImageLoading )
161 pixelBuffer = animatedImageLoading.LoadFrame( frameIndex );
166 pixelBuffer = LoadImageFromFile( mBrokenImageUrl );
170 pixelData = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
172 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, pixelData.GetPixelFormat(),
173 pixelData.GetWidth(), pixelData.GetHeight() );
174 texture.Upload( pixelData );
175 textureSet = TextureSet::New();
176 textureSet.SetTexture( 0u, texture );
180 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
183 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, pixelData.GetPixelFormat(),
184 pixelData.GetWidth(), pixelData.GetHeight() );
185 texture.Upload( pixelData );
186 textureSet = TextureSet::New();
187 textureSet.SetTexture( 0u, texture );
193 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
194 textureId = RequestLoadInternal( animatedImageLoading.GetUrl(), INVALID_TEXTURE_ID, 1.0f, ImageDimensions(), FittingMode::SCALE_TO_FILL,
195 SamplingMode::BOX_THEN_LINEAR, TextureManager::NO_ATLAS, false, UPLOAD_TO_TEXTURE, textureObserver,
196 true, TextureManager::ReloadPolicy::CACHED, preMultiply, animatedImageLoading, frameIndex );
197 TextureManager::LoadState loadState = GetTextureStateInternal( textureId );
198 if( loadState == TextureManager::UPLOADED )
200 // UploadComplete has already been called - keep the same texture set
201 textureSet = GetTextureSet( textureId );
207 Sampler sampler = Sampler::New();
208 sampler.SetWrapMode( wrapModeU, wrapModeV );
209 textureSet.SetSampler( 0u, sampler );
215 Devel::PixelBuffer TextureManager::LoadPixelBuffer(
216 const VisualUrl& url, Dali::ImageDimensions desiredSize, Dali::FittingMode::Type fittingMode, Dali::SamplingMode::Type samplingMode, bool synchronousLoading, TextureUploadObserver* textureObserver, bool orientationCorrection, TextureManager::MultiplyOnLoad& preMultiplyOnLoad )
218 Devel::PixelBuffer pixelBuffer;
219 if( synchronousLoading )
223 pixelBuffer = LoadImageFromFile( url.GetUrl(), desiredSize, fittingMode, samplingMode,
224 orientationCorrection );
225 if( pixelBuffer && preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD )
227 PreMultiply( pixelBuffer, preMultiplyOnLoad );
233 RequestLoadInternal( url, INVALID_TEXTURE_ID, 1.0f, desiredSize, fittingMode, samplingMode, TextureManager::NO_ATLAS,
234 false, RETURN_PIXEL_BUFFER, textureObserver, orientationCorrection, TextureManager::ReloadPolicy::FORCED,
235 preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u );
241 TextureSet TextureManager::LoadTexture(
242 const VisualUrl& url, Dali::ImageDimensions desiredSize, Dali::FittingMode::Type fittingMode,
243 Dali::SamplingMode::Type samplingMode, MaskingDataPointer& maskInfo,
244 bool synchronousLoading, TextureManager::TextureId& textureId, Vector4& textureRect,
245 Dali::ImageDimensions& textureRectSize, bool& atlasingStatus, bool& loadingStatus,
246 Dali::WrapMode::Type wrapModeU, Dali::WrapMode::Type wrapModeV, TextureUploadObserver* textureObserver,
247 AtlasUploadObserver* atlasObserver, ImageAtlasManagerPtr imageAtlasManager, bool orientationCorrection,
248 TextureManager::ReloadPolicy reloadPolicy, TextureManager::MultiplyOnLoad& preMultiplyOnLoad )
250 TextureSet textureSet;
252 loadingStatus = false;
253 textureRect = FULL_ATLAS_RECT;
255 if( VisualUrl::TEXTURE == url.GetProtocolType())
257 std::string location = url.GetLocation();
258 if( location.size() > 0u )
260 TextureId id = std::stoi( location );
261 for( auto&& elem : mExternalTextures )
263 if( elem.textureId == id )
265 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
266 textureId = elem.textureId;
267 return elem.textureSet;
272 else if( synchronousLoading )
277 Devel::PixelBuffer pixelBuffer = LoadImageFromFile( url.GetUrl(), desiredSize, fittingMode, samplingMode,
278 orientationCorrection );
279 if( maskInfo && maskInfo->mAlphaMaskUrl.IsValid() )
281 Devel::PixelBuffer maskPixelBuffer = LoadImageFromFile( maskInfo->mAlphaMaskUrl.GetUrl(), ImageDimensions(),
282 FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true );
283 if( maskPixelBuffer )
285 pixelBuffer.ApplyMask( maskPixelBuffer, maskInfo->mContentScaleFactor, maskInfo->mCropToMask );
290 PreMultiply( pixelBuffer, preMultiplyOnLoad );
291 data = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
297 Devel::PixelBuffer pixelBuffer = LoadImageFromFile( mBrokenImageUrl );
300 PreMultiply( pixelBuffer, preMultiplyOnLoad );
301 data = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
303 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, data.GetPixelFormat(),
304 data.GetWidth(), data.GetHeight() );
305 texture.Upload( data );
306 textureSet = TextureSet::New();
307 textureSet.SetTexture( 0u, texture );
311 if( atlasingStatus ) // attempt atlasing
313 textureSet = imageAtlasManager->Add( textureRect, data );
315 if( !textureSet ) // big image, no atlasing or atlasing failed
317 atlasingStatus = false;
318 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, data.GetPixelFormat(),
319 data.GetWidth(), data.GetHeight() );
320 texture.Upload( data );
321 textureSet = TextureSet::New();
322 textureSet.SetTexture( 0u, texture );
326 textureRectSize.SetWidth(data.GetWidth());
327 textureRectSize.SetHeight(data.GetHeight());
333 loadingStatus = true;
336 textureSet = imageAtlasManager->Add( textureRect, url.GetUrl(), desiredSize, fittingMode, true, atlasObserver);
338 if( !textureSet ) // big image, no atlasing or atlasing failed
340 atlasingStatus = false;
341 if( !maskInfo || !maskInfo->mAlphaMaskUrl.IsValid() )
343 textureId = RequestLoad( url, desiredSize, fittingMode, samplingMode, TextureManager::NO_ATLAS,
344 textureObserver, orientationCorrection, reloadPolicy, preMultiplyOnLoad );
348 maskInfo->mAlphaMaskId = RequestMaskLoad( maskInfo->mAlphaMaskUrl );
349 textureId = RequestLoad( url,
350 maskInfo->mAlphaMaskId,
351 maskInfo->mContentScaleFactor,
353 fittingMode, samplingMode,
354 TextureManager::NO_ATLAS,
355 maskInfo->mCropToMask,
357 orientationCorrection,
358 reloadPolicy, preMultiplyOnLoad );
361 TextureManager::LoadState loadState = GetTextureStateInternal( textureId );
362 if( loadState == TextureManager::UPLOADED )
364 // UploadComplete has already been called - keep the same texture set
365 textureSet = GetTextureSet( textureId );
368 // If we are loading the texture, or waiting for the ready signal handler to complete, inform
369 // caller that they need to wait.
370 loadingStatus = ( loadState == TextureManager::LOADING ||
371 loadState == TextureManager::WAITING_FOR_MASK ||
372 loadState == TextureManager::MASK_APPLYING ||
373 loadState == TextureManager::MASK_APPLIED ||
374 loadState == TextureManager::NOT_STARTED ||
380 textureRectSize = desiredSize;
384 if( ! atlasingStatus && textureSet )
386 Sampler sampler = Sampler::New();
387 sampler.SetWrapMode( wrapModeU, wrapModeV );
388 textureSet.SetSampler( 0u, sampler );
394 TextureManager::TextureId TextureManager::RequestLoad(
395 const VisualUrl& url,
396 const ImageDimensions desiredSize,
397 FittingMode::Type fittingMode,
398 Dali::SamplingMode::Type samplingMode,
399 const UseAtlas useAtlas,
400 TextureUploadObserver* observer,
401 bool orientationCorrection,
402 TextureManager::ReloadPolicy reloadPolicy,
403 TextureManager::MultiplyOnLoad& preMultiplyOnLoad )
405 return RequestLoadInternal( url, INVALID_TEXTURE_ID, 1.0f, desiredSize, fittingMode, samplingMode, useAtlas,
406 false, UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy,
407 preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u );
410 TextureManager::TextureId TextureManager::RequestLoad(
411 const VisualUrl& url,
412 TextureId maskTextureId,
414 const ImageDimensions desiredSize,
415 FittingMode::Type fittingMode,
416 Dali::SamplingMode::Type samplingMode,
417 const UseAtlas useAtlas,
419 TextureUploadObserver* observer,
420 bool orientationCorrection,
421 TextureManager::ReloadPolicy reloadPolicy,
422 TextureManager::MultiplyOnLoad& preMultiplyOnLoad )
424 return RequestLoadInternal( url, maskTextureId, contentScale, desiredSize, fittingMode, samplingMode, useAtlas,
425 cropToMask, UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy,
426 preMultiplyOnLoad, Dali::AnimatedImageLoading(), 0u );
429 TextureManager::TextureId TextureManager::RequestMaskLoad( const VisualUrl& maskUrl )
431 // Use the normal load procedure to get the alpha mask.
432 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
433 return RequestLoadInternal( maskUrl, INVALID_TEXTURE_ID, 1.0f, ImageDimensions(), FittingMode::SCALE_TO_FILL,
434 SamplingMode::NO_FILTER, NO_ATLAS, false, KEEP_PIXEL_BUFFER, NULL, true,
435 TextureManager::ReloadPolicy::CACHED, preMultiply, Dali::AnimatedImageLoading(), 0u );
438 TextureManager::TextureId TextureManager::RequestLoadInternal(
439 const VisualUrl& url,
440 TextureId maskTextureId,
442 const ImageDimensions desiredSize,
443 FittingMode::Type fittingMode,
444 Dali::SamplingMode::Type samplingMode,
447 StorageType storageType,
448 TextureUploadObserver* observer,
449 bool orientationCorrection,
450 TextureManager::ReloadPolicy reloadPolicy,
451 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
452 Dali::AnimatedImageLoading animatedImageLoading,
453 uint32_t frameIndex )
455 // First check if the requested Texture is cached.
456 bool isAnimatedImage = ( animatedImageLoading ) ? true : false;
457 const TextureHash textureHash = GenerateHash( url.GetUrl(), desiredSize, fittingMode, samplingMode, useAtlas,
458 maskTextureId, storageType, isAnimatedImage, frameIndex );
460 TextureManager::TextureId textureId = INVALID_TEXTURE_ID;
461 // Look up the texture by hash. Note: The extra parameters are used in case of a hash collision.
462 int cacheIndex = FindCachedTexture( textureHash, url.GetUrl(), desiredSize, fittingMode, samplingMode, useAtlas,
463 maskTextureId, preMultiplyOnLoad, storageType, isAnimatedImage, frameIndex );
465 // Check if the requested Texture exists in the cache.
466 if( cacheIndex != INVALID_CACHE_INDEX )
468 if ( TextureManager::ReloadPolicy::CACHED == reloadPolicy )
470 // Mark this texture being used by another client resource. Forced reload would replace the current texture
471 // without the need for incrementing the reference count.
472 ++( mTextureInfoContainer[ cacheIndex ].referenceCount );
474 textureId = mTextureInfoContainer[ cacheIndex ].textureId;
476 // Update preMultiplyOnLoad value. It should be changed according to preMultiplied value of the cached info.
477 preMultiplyOnLoad = mTextureInfoContainer[ cacheIndex ].preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
479 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::General, "TextureManager::RequestLoad( url=%s observer=%p ) Using cached texture id@%d, textureId=%d\n",
480 url.GetUrl().c_str(), observer, cacheIndex, textureId );
483 if( textureId == INVALID_TEXTURE_ID ) // There was no caching, or caching not required
485 // We need a new Texture.
486 textureId = GenerateUniqueTextureId();
487 bool preMultiply = ( preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD );
488 mTextureInfoContainer.push_back( TextureInfo( textureId, maskTextureId, url.GetUrl(),
489 desiredSize, contentScale, fittingMode, samplingMode,
490 false, cropToMask, useAtlas, textureHash, orientationCorrection,
491 preMultiply, animatedImageLoading, frameIndex ) );
492 cacheIndex = mTextureInfoContainer.size() - 1u;
494 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::General, "TextureManager::RequestLoad( url=%s observer=%p ) New texture, cacheIndex:%d, textureId=%d\n",
495 url.GetUrl().c_str(), observer, cacheIndex, textureId );
498 // The below code path is common whether we are using the cache or not.
499 // The textureInfoIndex now refers to either a pre-existing cached TextureInfo,
500 // or a new TextureInfo just created.
501 TextureInfo& textureInfo( mTextureInfoContainer[ cacheIndex ] );
502 textureInfo.maskTextureId = maskTextureId;
503 textureInfo.storageType = storageType;
504 textureInfo.orientationCorrection = orientationCorrection;
506 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::General, "TextureInfo loadState:%s\n",
507 GET_LOAD_STATE_STRING(textureInfo.loadState ) );
509 // Force reloading of texture by setting loadState unless already loading or cancelled.
510 if ( TextureManager::ReloadPolicy::FORCED == reloadPolicy &&
511 TextureManager::LOADING != textureInfo.loadState &&
512 TextureManager::WAITING_FOR_MASK != textureInfo.loadState &&
513 TextureManager::MASK_APPLYING != textureInfo.loadState &&
514 TextureManager::MASK_APPLIED != textureInfo.loadState &&
515 TextureManager::CANCELLED != textureInfo.loadState )
517 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Verbose, "TextureManager::RequestLoad( url=%s observer=%p ) ForcedReload cacheIndex:%d, textureId=%d\n",
518 url.GetUrl().c_str(), observer, cacheIndex, textureId );
520 textureInfo.loadState = TextureManager::NOT_STARTED;
523 // Check if we should add the observer.
524 // Only do this if we have not loaded yet and it will not have loaded by the end of this method.
525 switch( textureInfo.loadState )
527 case TextureManager::LOAD_FAILED: // Failed notifies observer which then stops observing.
528 case TextureManager::NOT_STARTED:
530 LoadOrQueueTexture( textureInfo, observer ); // If called inside NotifyObservers, queues until afterwards
533 case TextureManager::LOADING:
534 case TextureManager::WAITING_FOR_MASK:
535 case TextureManager::MASK_APPLYING:
536 case TextureManager::MASK_APPLIED:
538 ObserveTexture( textureInfo, observer );
541 case TextureManager::UPLOADED:
545 LoadOrQueueTexture( textureInfo, observer );
549 case TextureManager::CANCELLED:
551 // A cancelled texture hasn't finished loading yet. Treat as a loading texture
552 // (it's ref count has already been incremented, above)
553 textureInfo.loadState = TextureManager::LOADING;
554 ObserveTexture( textureInfo, observer );
557 case TextureManager::LOAD_FINISHED:
559 // Loading has already completed.
560 if( observer && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER )
562 LoadOrQueueTexture( textureInfo, observer );
568 // Return the TextureId for which this Texture can now be referenced by externally.
572 void TextureManager::Remove( const TextureManager::TextureId textureId, TextureUploadObserver* observer )
574 int textureInfoIndex = GetCacheIndexFromId( textureId );
575 if( textureInfoIndex != INVALID_INDEX )
577 TextureInfo& textureInfo( mTextureInfoContainer[ textureInfoIndex ] );
579 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise,
580 "TextureManager::Remove(%d) url:%s\n cacheIdx:%d loadState:%s reference count = %d\n",
581 textureId, textureInfo.url.GetUrl().c_str(),
582 textureInfoIndex, GET_LOAD_STATE_STRING( textureInfo.loadState ), textureInfo.referenceCount );
584 // Decrement the reference count and check if this is the last user of this Texture.
585 if( --textureInfo.referenceCount <= 0 )
587 // This is the last remove for this Texture.
588 textureInfo.referenceCount = 0;
589 bool removeTextureInfo = false;
591 // If loaded, we can remove the TextureInfo and the Atlas (if atlased).
592 if( textureInfo.loadState == UPLOADED )
594 if( textureInfo.atlas )
596 textureInfo.atlas.Remove( textureInfo.atlasRect );
598 removeTextureInfo = true;
600 else if( textureInfo.loadState == LOADING )
602 // We mark the textureInfo for removal.
603 // Once the load has completed, this method will be called again.
604 textureInfo.loadState = CANCELLED;
608 // In other states, we are not waiting for a load so we are safe to remove the TextureInfo data.
609 removeTextureInfo = true;
612 // If the state allows us to remove the TextureInfo data, we do so.
613 if( removeTextureInfo )
615 // Permanently remove the textureInfo struct.
616 mTextureInfoContainer.erase( mTextureInfoContainer.begin() + textureInfoIndex );
622 // Remove element from the LoadQueue
623 for( auto&& element : mLoadQueue )
625 if( element.mObserver == observer )
627 mLoadQueue.Erase( &element );
635 VisualUrl TextureManager::GetVisualUrl( TextureId textureId )
637 VisualUrl visualUrl("");
638 int cacheIndex = GetCacheIndexFromId( textureId );
640 if( cacheIndex != INVALID_CACHE_INDEX )
642 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::GetVisualUrl. Using cached texture id=%d, textureId=%d\n",
643 cacheIndex, textureId );
645 TextureInfo& cachedTextureInfo( mTextureInfoContainer[ cacheIndex ] );
646 visualUrl = cachedTextureInfo.url;
651 TextureManager::LoadState TextureManager::GetTextureState( TextureId textureId )
653 LoadState loadState = TextureManager::NOT_STARTED;
655 int cacheIndex = GetCacheIndexFromId( textureId );
656 if( cacheIndex != INVALID_CACHE_INDEX )
658 TextureInfo& cachedTextureInfo( mTextureInfoContainer[ cacheIndex ] );
659 loadState = cachedTextureInfo.loadState;
663 for( auto&& elem : mExternalTextures )
665 if( elem.textureId == textureId )
667 loadState = LoadState::UPLOADED;
675 TextureManager::LoadState TextureManager::GetTextureStateInternal( TextureId textureId )
677 LoadState loadState = TextureManager::NOT_STARTED;
679 int cacheIndex = GetCacheIndexFromId( textureId );
680 if( cacheIndex != INVALID_CACHE_INDEX )
682 TextureInfo& cachedTextureInfo( mTextureInfoContainer[ cacheIndex ] );
683 loadState = cachedTextureInfo.loadState;
689 TextureSet TextureManager::GetTextureSet( TextureId textureId )
691 TextureSet textureSet;// empty handle
693 int cacheIndex = GetCacheIndexFromId( textureId );
694 if( cacheIndex != INVALID_CACHE_INDEX )
696 TextureInfo& cachedTextureInfo( mTextureInfoContainer[ cacheIndex ] );
697 textureSet = cachedTextureInfo.textureSet;
701 for( auto&& elem : mExternalTextures )
703 if( elem.textureId == textureId )
705 textureSet = elem.textureSet;
713 std::string TextureManager::AddExternalTexture( TextureSet& textureSet )
715 TextureManager::ExternalTextureInfo info;
716 info.textureId = GenerateUniqueTextureId();
717 info.textureSet = textureSet;
718 mExternalTextures.emplace_back( info );
719 return VisualUrl::CreateTextureUrl( std::to_string( info.textureId ) );
722 TextureSet TextureManager::RemoveExternalTexture( const std::string& url )
724 if( url.size() > 0u )
726 // get the location from the Url
727 VisualUrl parseUrl( url );
728 if( VisualUrl::TEXTURE == parseUrl.GetProtocolType() )
730 std::string location = parseUrl.GetLocation();
731 if( location.size() > 0u )
733 TextureId id = std::stoi( location );
734 const auto end = mExternalTextures.end();
735 for( auto iter = mExternalTextures.begin(); iter != end; ++iter )
737 if( iter->textureId == id )
739 auto textureSet = iter->textureSet;
740 mExternalTextures.erase( iter );
750 void TextureManager::AddObserver( TextureManager::LifecycleObserver& observer )
752 // make sure an observer doesn't observe the same object twice
753 // otherwise it will get multiple calls to ObjectDestroyed()
754 DALI_ASSERT_DEBUG( mLifecycleObservers.End() == std::find( mLifecycleObservers.Begin(), mLifecycleObservers.End(), &observer));
755 mLifecycleObservers.PushBack( &observer );
758 void TextureManager::RemoveObserver( TextureManager::LifecycleObserver& observer)
760 // Find the observer...
761 auto endIter = mLifecycleObservers.End();
762 for( auto iter = mLifecycleObservers.Begin(); iter != endIter; ++iter)
764 if( (*iter) == &observer)
766 mLifecycleObservers.Erase( iter );
770 DALI_ASSERT_DEBUG(endIter != mLifecycleObservers.End());
773 void TextureManager::LoadOrQueueTexture( TextureInfo& textureInfo, TextureUploadObserver* observer )
775 switch( textureInfo.loadState )
782 QueueLoadTexture( textureInfo, observer );
786 LoadTexture( textureInfo, observer );
794 QueueLoadTexture( textureInfo, observer );
798 // The Texture has already loaded. The other observers have already been notified.
799 // We need to send a "late" loaded notification for this observer.
800 observer->UploadComplete( true, textureInfo.textureId, textureInfo.textureSet,
801 textureInfo.useAtlas, textureInfo.atlasRect,
802 textureInfo.preMultiplied );
809 case WAITING_FOR_MASK:
818 void TextureManager::QueueLoadTexture( TextureInfo& textureInfo, TextureUploadObserver* observer )
820 auto textureId = textureInfo.textureId;
821 mLoadQueue.PushBack( LoadQueueElement( textureId, observer) );
823 observer->DestructionSignal().Connect( this, &TextureManager::ObserverDestroyed );
826 void TextureManager::LoadTexture( TextureInfo& textureInfo, TextureUploadObserver* observer )
828 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::LoadTexture(): url:%s sync:%s\n",
829 textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously?"T":"F" );
831 textureInfo.loadState = LOADING;
832 if( !textureInfo.loadSynchronously )
834 auto& loadersContainer = textureInfo.url.IsLocalResource() ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
835 auto loadingHelperIt = loadersContainer.GetNext();
836 auto premultiplyOnLoad = ( textureInfo.preMultiplyOnLoad && textureInfo.maskTextureId == INVALID_TEXTURE_ID ) ?
837 DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
838 DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
839 if( textureInfo.animatedImageLoading )
841 loadingHelperIt->LoadAnimatedImage( textureInfo.textureId, textureInfo.animatedImageLoading, textureInfo.frameIndex );
845 loadingHelperIt->Load(textureInfo.textureId, textureInfo.url,
846 textureInfo.desiredSize, textureInfo.fittingMode,
847 textureInfo.samplingMode, textureInfo.orientationCorrection,
851 ObserveTexture( textureInfo, observer );
854 void TextureManager::ProcessQueuedTextures()
856 for( auto&& element : mLoadQueue )
858 if( !element.mObserver )
863 int cacheIndex = GetCacheIndexFromId( element.mTextureId );
864 if( cacheIndex != INVALID_CACHE_INDEX )
866 TextureInfo& textureInfo( mTextureInfoContainer[cacheIndex] );
867 if( textureInfo.loadState == UPLOADED )
869 element.mObserver->UploadComplete( true, textureInfo.textureId, textureInfo.textureSet,
870 textureInfo.useAtlas, textureInfo.atlasRect,
871 textureInfo.preMultiplied );
873 else if ( textureInfo.loadState == LOAD_FINISHED && textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER )
875 element.mObserver->LoadComplete( true, textureInfo.pixelBuffer, textureInfo.url, textureInfo.preMultiplied );
879 LoadTexture( textureInfo, element.mObserver );
886 void TextureManager::ObserveTexture( TextureInfo& textureInfo,
887 TextureUploadObserver* observer )
889 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::ObserveTexture(): url:%s observer:%p\n",
890 textureInfo.url.GetUrl().c_str(), observer );
894 textureInfo.observerList.PushBack( observer );
895 observer->DestructionSignal().Connect( this, &TextureManager::ObserverDestroyed );
899 void TextureManager::AsyncLoadComplete( AsyncLoadingInfoContainerType& loadingContainer, uint32_t id,
900 Devel::PixelBuffer pixelBuffer )
902 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::AsyncLoadComplete( id:%d )\n", id );
904 if( loadingContainer.size() >= 1u )
906 AsyncLoadingInfo loadingInfo = loadingContainer.front();
908 if( loadingInfo.loadId == id )
910 int cacheIndex = GetCacheIndexFromId( loadingInfo.textureId );
911 if( cacheIndex != INVALID_CACHE_INDEX )
913 TextureInfo& textureInfo( mTextureInfoContainer[cacheIndex] );
915 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise,
916 " textureId:%d Url:%s CacheIndex:%d LoadState: %d\n",
917 textureInfo.textureId, textureInfo.url.GetUrl().c_str(), cacheIndex, textureInfo.loadState );
919 if( textureInfo.loadState != CANCELLED )
921 // textureInfo can be invalidated after this call (as the mTextureInfoContainer may be modified)
922 PostLoad( textureInfo, pixelBuffer );
926 Remove( textureInfo.textureId, nullptr );
931 loadingContainer.pop_front();
935 void TextureManager::PostLoad( TextureInfo& textureInfo, Devel::PixelBuffer& pixelBuffer )
937 // Was the load successful?
938 if( pixelBuffer && ( pixelBuffer.GetWidth() != 0 ) && ( pixelBuffer.GetHeight() != 0 ) )
940 // No atlas support for now
941 textureInfo.useAtlas = NO_ATLAS;
942 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
944 if( textureInfo.storageType == UPLOAD_TO_TEXTURE )
946 // If there is a mask texture ID associated with this texture, then apply the mask
947 // if it's already loaded. If it hasn't, and the mask is still loading,
948 // wait for the mask to finish loading.
949 if( textureInfo.maskTextureId != INVALID_TEXTURE_ID )
951 if( textureInfo.loadState == MASK_APPLYING )
953 textureInfo.loadState = MASK_APPLIED;
954 UploadTexture( pixelBuffer, textureInfo );
955 NotifyObservers( textureInfo, true );
959 LoadState maskLoadState = GetTextureStateInternal( textureInfo.maskTextureId );
960 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
961 if( maskLoadState == LOADING )
963 textureInfo.loadState = WAITING_FOR_MASK;
965 else if( maskLoadState == LOAD_FINISHED )
967 // Send New Task to Thread
968 ApplyMask( textureInfo, textureInfo.maskTextureId );
974 UploadTexture( pixelBuffer, textureInfo );
975 NotifyObservers( textureInfo, true );
980 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
981 textureInfo.loadState = LOAD_FINISHED;
983 if( textureInfo.storageType == StorageType::RETURN_PIXEL_BUFFER )
985 NotifyObservers( textureInfo, true );
989 // Check if there was another texture waiting for this load to complete
990 // (e.g. if this was an image mask, and its load is on a different thread)
991 CheckForWaitingTexture( textureInfo );
997 // @todo If the load was unsuccessful, upload the broken image.
998 textureInfo.loadState = LOAD_FAILED;
999 CheckForWaitingTexture( textureInfo );
1000 NotifyObservers( textureInfo, false );
1004 void TextureManager::CheckForWaitingTexture( TextureInfo& maskTextureInfo )
1006 // Search the cache, checking if any texture has this texture id as a
1008 const unsigned int size = mTextureInfoContainer.size();
1010 for( unsigned int cacheIndex = 0; cacheIndex < size; ++cacheIndex )
1012 if( mTextureInfoContainer[cacheIndex].maskTextureId == maskTextureInfo.textureId &&
1013 mTextureInfoContainer[cacheIndex].loadState == WAITING_FOR_MASK )
1015 TextureInfo& textureInfo( mTextureInfoContainer[cacheIndex] );
1017 if( maskTextureInfo.loadState == LOAD_FINISHED )
1019 // Send New Task to Thread
1020 ApplyMask( textureInfo, maskTextureInfo.textureId );
1024 textureInfo.pixelBuffer.Reset();
1025 textureInfo.loadState = LOAD_FAILED;
1026 NotifyObservers( textureInfo, false );
1032 void TextureManager::ApplyMask( TextureInfo& textureInfo, TextureId maskTextureId )
1034 int maskCacheIndex = GetCacheIndexFromId( maskTextureId );
1035 if( maskCacheIndex != INVALID_CACHE_INDEX )
1037 Devel::PixelBuffer maskPixelBuffer = mTextureInfoContainer[maskCacheIndex].pixelBuffer;
1038 Devel::PixelBuffer pixelBuffer = textureInfo.pixelBuffer;
1039 textureInfo.pixelBuffer.Reset();
1041 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::ApplyMask(): url:%s sync:%s\n",
1042 textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously?"T":"F" );
1044 textureInfo.loadState = MASK_APPLYING;
1045 auto& loadersContainer = textureInfo.url.IsLocalResource() ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
1046 auto loadingHelperIt = loadersContainer.GetNext();
1047 auto premultiplyOnLoad = textureInfo.preMultiplyOnLoad ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
1048 DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
1049 loadingHelperIt->ApplyMask( textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask, premultiplyOnLoad );
1053 void TextureManager::UploadTexture( Devel::PixelBuffer& pixelBuffer, TextureInfo& textureInfo )
1055 if( textureInfo.useAtlas != USE_ATLAS )
1057 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::General, " TextureManager::UploadTexture() New Texture for textureId:%d\n", textureInfo.textureId );
1059 // Check if this pixelBuffer is premultiplied
1060 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
1062 auto& renderingAddOn = RenderingAddOn::Get();
1063 if( renderingAddOn.IsValid() )
1065 renderingAddOn.CreateGeometry( textureInfo.textureId, pixelBuffer );
1068 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(),
1069 pixelBuffer.GetWidth(), pixelBuffer.GetHeight() );
1071 PixelData pixelData = Devel::PixelBuffer::Convert( pixelBuffer );
1072 texture.Upload( pixelData );
1073 if ( ! textureInfo.textureSet )
1075 textureInfo.textureSet = TextureSet::New();
1077 textureInfo.textureSet.SetTexture( 0u, texture );
1080 // Update the load state.
1081 // Note: This is regardless of success as we care about whether a
1082 // load attempt is in progress or not. If unsuccessful, a broken
1083 // image is still loaded.
1084 textureInfo.loadState = UPLOADED;
1087 void TextureManager::NotifyObservers( TextureInfo& textureInfo, bool success )
1089 TextureId textureId = textureInfo.textureId;
1091 // If there is an observer: Notify the load is complete, whether successful or not,
1092 // and erase it from the list
1093 TextureInfo* info = &textureInfo;
1095 mQueueLoadFlag = true;
1097 while( info->observerList.Count() )
1099 TextureUploadObserver* observer = info->observerList[0];
1101 // During UploadComplete() a Control ResourceReady() signal is emitted.
1102 // During that signal the app may add remove /add Textures (e.g. via
1104 // It is possible for observers to be removed from the observer list,
1105 // and it is also possible for the mTextureInfoContainer to be modified,
1106 // invalidating the reference to the textureInfo struct.
1107 // Texture load requests for the same URL are deferred until the end of this
1109 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "NotifyObservers() url:%s loadState:%s\n",
1110 textureInfo.url.GetUrl().c_str(), GET_LOAD_STATE_STRING(textureInfo.loadState ) );
1112 // It is possible for the observer to be deleted.
1113 // Disconnect and remove the observer first.
1114 observer->DestructionSignal().Disconnect( this, &TextureManager::ObserverDestroyed );
1116 info->observerList.Erase( info->observerList.begin() );
1118 if( info->storageType == StorageType::RETURN_PIXEL_BUFFER )
1120 observer->LoadComplete( success, info->pixelBuffer, info->url, info->preMultiplied );
1124 observer->UploadComplete( success, info->textureId, info->textureSet, info->useAtlas, info->atlasRect,
1125 info->preMultiplied );
1128 // Get the textureInfo from the container again as it may have been invalidated.
1129 int textureInfoIndex = GetCacheIndexFromId( textureId );
1130 if( textureInfoIndex == INVALID_CACHE_INDEX)
1132 break; // texture has been removed - can stop.
1134 info = &mTextureInfoContainer[ textureInfoIndex ];
1137 mQueueLoadFlag = false;
1138 ProcessQueuedTextures();
1140 if( info->storageType == StorageType::RETURN_PIXEL_BUFFER && info->observerList.Count() == 0 )
1142 Remove( info->textureId, nullptr );
1146 TextureManager::TextureId TextureManager::GenerateUniqueTextureId()
1148 return mCurrentTextureId++;
1151 int TextureManager::GetCacheIndexFromId( const TextureId textureId )
1153 const unsigned int size = mTextureInfoContainer.size();
1155 for( unsigned int i = 0; i < size; ++i )
1157 if( mTextureInfoContainer[i].textureId == textureId )
1163 return INVALID_CACHE_INDEX;
1166 TextureManager::TextureHash TextureManager::GenerateHash(
1167 const std::string& url,
1168 const ImageDimensions size,
1169 const FittingMode::Type fittingMode,
1170 const Dali::SamplingMode::Type samplingMode,
1171 const UseAtlas useAtlas,
1172 TextureId maskTextureId,
1173 StorageType storageType,
1174 bool isAnimationImage,
1175 uint32_t frameIndex )
1177 std::string hashTarget( url );
1178 const size_t urlLength = hashTarget.length();
1179 const uint16_t width = size.GetWidth();
1180 const uint16_t height = size.GetWidth();
1182 // If either the width or height has been specified, include the resizing options in the hash
1183 if( width != 0 || height != 0 )
1185 // We are appending 5 bytes to the URL to form the hash input.
1186 hashTarget.resize( urlLength + 5u );
1187 char* hashTargetPtr = &( hashTarget[ urlLength ] );
1189 // Pack the width and height (4 bytes total).
1190 *hashTargetPtr++ = size.GetWidth() & 0xff;
1191 *hashTargetPtr++ = ( size.GetWidth() >> 8u ) & 0xff;
1192 *hashTargetPtr++ = size.GetHeight() & 0xff;
1193 *hashTargetPtr++ = ( size.GetHeight() >> 8u ) & 0xff;
1195 // Bit-pack the FittingMode, SamplingMode and atlasing.
1196 // FittingMode=2bits, SamplingMode=3bits, useAtlas=1bit, storageType=2bits
1197 *hashTargetPtr = ( fittingMode << 6u ) | ( samplingMode << 3 ) | ( useAtlas << 2 ) | storageType;
1201 // We are not including sizing information, but we still need an extra byte for atlasing.
1202 hashTarget.resize( urlLength + 1u );
1204 // Add the atlasing to the hash input.
1207 case UseAtlas::NO_ATLAS:
1209 hashTarget[ urlLength ] = 'f';
1212 case UseAtlas::USE_ATLAS:
1214 hashTarget[ urlLength ] = 't';
1220 if( isAnimationImage )
1222 auto textureIdIndex = hashTarget.length();
1223 hashTarget.resize( hashTarget.length() + sizeof( uint32_t ) );
1224 char* hashTargetPtr = &( hashTarget[ textureIdIndex ] );
1226 for( size_t byteIter = 0; byteIter < sizeof( uint32_t ); ++byteIter )
1228 *hashTargetPtr++ = frameIndex & 0xff;
1233 if( maskTextureId != INVALID_TEXTURE_ID )
1235 auto textureIdIndex = hashTarget.length();
1236 hashTarget.resize( hashTarget.length() + sizeof( TextureId ) );
1237 unsigned char* hashTargetPtr = reinterpret_cast<unsigned char*>(&( hashTarget[ textureIdIndex ] ));
1239 // Append the texture id to the end of the URL byte by byte:
1240 // (to avoid SIGBUS / alignment issues)
1241 for( size_t byteIter = 0; byteIter < sizeof( TextureId ); ++byteIter )
1243 *hashTargetPtr++ = maskTextureId & 0xff;
1244 maskTextureId >>= 8u;
1248 return Dali::CalculateHash( hashTarget );
1251 int TextureManager::FindCachedTexture(
1252 const TextureManager::TextureHash hash,
1253 const std::string& url,
1254 const ImageDimensions size,
1255 const FittingMode::Type fittingMode,
1256 const Dali::SamplingMode::Type samplingMode,
1257 const bool useAtlas,
1258 TextureId maskTextureId,
1259 TextureManager::MultiplyOnLoad preMultiplyOnLoad,
1260 StorageType storageType,
1261 bool isAnimatedImage,
1262 uint32_t frameIndex )
1264 // Default to an invalid ID, in case we do not find a match.
1265 int cacheIndex = INVALID_CACHE_INDEX;
1267 // Iterate through our hashes to find a match.
1268 const unsigned int count = mTextureInfoContainer.size();
1269 for( unsigned int i = 0u; i < count; ++i )
1271 if( mTextureInfoContainer[i].hash == hash )
1273 // We have a match, now we check all the original parameters in case of a hash collision.
1274 TextureInfo& textureInfo( mTextureInfoContainer[i] );
1276 if( ( url == textureInfo.url.GetUrl() ) &&
1277 ( useAtlas == textureInfo.useAtlas ) &&
1278 ( maskTextureId == textureInfo.maskTextureId ) &&
1279 ( size == textureInfo.desiredSize ) &&
1280 ( ( size.GetWidth() == 0 && size.GetHeight() == 0 ) ||
1281 ( fittingMode == textureInfo.fittingMode &&
1282 samplingMode == textureInfo.samplingMode ) ) &&
1283 ( storageType == textureInfo.storageType ) &&
1284 ( isAnimatedImage == ( ( textureInfo.animatedImageLoading ) ? true : false ) ) &&
1285 ( frameIndex == textureInfo.frameIndex ) )
1287 // 1. If preMultiplyOnLoad is MULTIPLY_ON_LOAD, then textureInfo.preMultiplyOnLoad should be true. The premultiplication result can be different.
1288 // 2. If preMultiplyOnLoad is LOAD_WITHOUT_MULTIPLY, then textureInfo.preMultiplied should be false.
1289 if( ( preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD && textureInfo.preMultiplyOnLoad )
1290 || ( preMultiplyOnLoad == TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY && !textureInfo.preMultiplied ) )
1292 // The found Texture is a match.
1303 void TextureManager::ObserverDestroyed( TextureUploadObserver* observer )
1305 const unsigned int count = mTextureInfoContainer.size();
1306 for( unsigned int i = 0; i < count; ++i )
1308 TextureInfo& textureInfo( mTextureInfoContainer[i] );
1309 for( TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin();
1310 j != textureInfo.observerList.End(); )
1312 if( *j == observer )
1314 j = textureInfo.observerList.Erase( j );
1323 // Remove element from the LoadQueue
1324 for( auto&& element : mLoadQueue )
1326 if( element.mObserver == observer )
1328 element.mObserver = nullptr;
1334 TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(TextureManager& textureManager)
1335 : AsyncLoadingHelper(Toolkit::AsyncImageLoader::New(), textureManager,
1336 AsyncLoadingInfoContainerType())
1340 void TextureManager::AsyncLoadingHelper::LoadAnimatedImage( TextureId textureId,
1341 Dali::AnimatedImageLoading animatedImageLoading,
1342 uint32_t frameIndex )
1344 mLoadingInfoContainer.push_back( AsyncLoadingInfo( textureId ) );
1345 auto id = DevelAsyncImageLoader::LoadAnimatedImage( mLoader, animatedImageLoading, frameIndex );
1346 mLoadingInfoContainer.back().loadId = id;
1349 void TextureManager::AsyncLoadingHelper::Load( TextureId textureId,
1350 const VisualUrl& url,
1351 ImageDimensions desiredSize,
1352 FittingMode::Type fittingMode,
1353 SamplingMode::Type samplingMode,
1354 bool orientationCorrection,
1355 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad )
1357 mLoadingInfoContainer.push_back( AsyncLoadingInfo( textureId ) );
1358 auto id = DevelAsyncImageLoader::Load( mLoader, url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad );
1359 mLoadingInfoContainer.back().loadId = id;
1362 void TextureManager::AsyncLoadingHelper::ApplyMask( TextureId textureId,
1363 Devel::PixelBuffer pixelBuffer,
1364 Devel::PixelBuffer maskPixelBuffer,
1367 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad )
1369 mLoadingInfoContainer.push_back( AsyncLoadingInfo( textureId ) );
1370 auto id = DevelAsyncImageLoader::ApplyMask( mLoader, pixelBuffer, maskPixelBuffer, contentScale, cropToMask, preMultiplyOnLoad );
1371 mLoadingInfoContainer.back().loadId = id;
1374 TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(AsyncLoadingHelper&& rhs)
1375 : AsyncLoadingHelper(rhs.mLoader, rhs.mTextureManager, std::move(rhs.mLoadingInfoContainer))
1379 TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(
1380 Toolkit::AsyncImageLoader loader,
1381 TextureManager& textureManager,
1382 AsyncLoadingInfoContainerType&& loadingInfoContainer)
1384 mTextureManager(textureManager),
1385 mLoadingInfoContainer(std::move(loadingInfoContainer))
1387 DevelAsyncImageLoader::PixelBufferLoadedSignal(mLoader).Connect(
1388 this, &AsyncLoadingHelper::AsyncLoadComplete);
1391 void TextureManager::AsyncLoadingHelper::AsyncLoadComplete(uint32_t id,
1392 Devel::PixelBuffer pixelBuffer )
1394 mTextureManager.AsyncLoadComplete( mLoadingInfoContainer, id, pixelBuffer );
1397 void TextureManager::SetBrokenImageUrl(const std::string& brokenImageUrl)
1399 mBrokenImageUrl = brokenImageUrl;
1402 Geometry TextureManager::GetRenderGeometry(TextureId textureId, uint32_t& frontElements, uint32_t& backElements )
1404 return RenderingAddOn::Get().IsValid() ?
1405 RenderingAddOn::Get().GetGeometry( textureId, frontElements, backElements) :
1409 } // namespace Internal
1411 } // namespace Toolkit