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/images/texture-set-image.h>
29 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
30 #include <dali/integration-api/debug.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>
40 constexpr auto DEFAULT_NUMBER_OF_LOCAL_LOADER_THREADS = size_t{4u};
41 constexpr auto DEFAULT_NUMBER_OF_REMOTE_LOADER_THREADS = size_t{8u};
43 constexpr auto NUMBER_OF_LOCAL_LOADER_THREADS_ENV = "DALI_TEXTURE_LOCAL_THREADS";
44 constexpr auto NUMBER_OF_REMOTE_LOADER_THREADS_ENV = "DALI_TEXTURE_REMOTE_THREADS";
46 size_t GetNumberOfThreads(const char* environmentVariable, size_t defaultValue)
48 using Dali::EnvironmentVariable::GetEnvironmentVariable;
49 auto numberString = GetEnvironmentVariable(environmentVariable);
50 auto numberOfThreads = numberString ? std::strtoul(numberString, nullptr, 10) : 0;
51 constexpr auto MAX_NUMBER_OF_THREADS = 100u;
52 DALI_ASSERT_DEBUG( numberOfThreads < MAX_NUMBER_OF_THREADS );
53 return ( numberOfThreads > 0 && numberOfThreads < MAX_NUMBER_OF_THREADS ) ? numberOfThreads : defaultValue;
56 size_t GetNumberOfLocalLoaderThreads()
58 return GetNumberOfThreads(NUMBER_OF_LOCAL_LOADER_THREADS_ENV, DEFAULT_NUMBER_OF_LOCAL_LOADER_THREADS);
61 size_t GetNumberOfRemoteLoaderThreads()
63 return GetNumberOfThreads(NUMBER_OF_REMOTE_LOADER_THREADS_ENV, DEFAULT_NUMBER_OF_REMOTE_LOADER_THREADS);
81 Debug::Filter* gTextureManagerLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_TEXTURE_MANAGER" );
83 #define GET_LOAD_STATE_STRING( loadState ) \
84 loadState == TextureManager::NOT_STARTED ? "NOT_STARTED" : \
85 loadState == TextureManager::LOADING ? "LOADING" : \
86 loadState == TextureManager::LOAD_FINISHED ? "LOAD_FINISHED" : \
87 loadState == TextureManager::WAITING_FOR_MASK ? "WAITING_FOR_MASK" : \
88 loadState == TextureManager::MASK_APPLYING ? "MASK_APPLYING" : \
89 loadState == TextureManager::MASK_APPLIED ? "MASK_APPLIED" : \
90 loadState == TextureManager::UPLOADED ? "UPLOADED" : \
91 loadState == TextureManager::CANCELLED ? "CANCELLED" : \
92 loadState == TextureManager::LOAD_FAILED ? "LOAD_FAILED" : "Unknown"
96 const uint32_t DEFAULT_ATLAS_SIZE( 1024u ); ///< This size can fit 8 by 8 images of average size 128 * 128
97 const Vector4 FULL_ATLAS_RECT( 0.0f, 0.0f, 1.0f, 1.0f ); ///< UV Rectangle that covers the full Texture
98 const int INVALID_INDEX( -1 ); ///< Invalid index used to represent a non-existant TextureInfo struct
99 const int INVALID_CACHE_INDEX( -1 ); ///< Invalid Cache index
102 void PreMultiply( Devel::PixelBuffer pixelBuffer, TextureManager::MultiplyOnLoad& preMultiplyOnLoad )
104 if( Pixel::HasAlpha( pixelBuffer.GetPixelFormat() ) )
106 if( preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD )
108 pixelBuffer.MultiplyColorByAlpha();
113 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
117 } // Anonymous namespace
119 TextureManager::MaskingData::MaskingData()
121 mAlphaMaskId( INVALID_TEXTURE_ID ),
122 mContentScaleFactor( 1.0f ),
127 TextureManager::TextureManager()
128 : mAsyncLocalLoaders( GetNumberOfLocalLoaderThreads(), [&]() { return AsyncLoadingHelper(*this); } ),
129 mAsyncRemoteLoaders( GetNumberOfRemoteLoaderThreads(), [&]() { return AsyncLoadingHelper(*this); } ),
131 mLifecycleObservers(),
134 mCurrentTextureId( 0 ),
135 mQueueLoadFlag(false)
139 TextureManager::~TextureManager()
141 for( auto iter = mLifecycleObservers.Begin(), endIter = mLifecycleObservers.End(); iter != endIter; ++iter)
143 (*iter)->TextureManagerDestroyed();
147 Devel::PixelBuffer TextureManager::LoadPixelBuffer(
148 const VisualUrl& url, Dali::ImageDimensions desiredSize, Dali::FittingMode::Type fittingMode, Dali::SamplingMode::Type samplingMode, bool synchronousLoading, TextureUploadObserver* textureObserver, bool orientationCorrection, TextureManager::MultiplyOnLoad& preMultiplyOnLoad )
150 Devel::PixelBuffer pixelBuffer;
151 if( synchronousLoading )
155 pixelBuffer = LoadImageFromFile( url.GetUrl(), desiredSize, fittingMode, samplingMode,
156 orientationCorrection );
157 if( pixelBuffer && preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD )
159 PreMultiply( pixelBuffer, preMultiplyOnLoad );
165 RequestLoadInternal( url, INVALID_TEXTURE_ID, 1.0f, desiredSize, fittingMode, samplingMode, TextureManager::NO_ATLAS,
166 false, KEEP_PIXEL_BUFFER, textureObserver, orientationCorrection, TextureManager::ReloadPolicy::FORCED,
167 preMultiplyOnLoad, true );
173 TextureSet TextureManager::LoadTexture(
174 const VisualUrl& url, Dali::ImageDimensions desiredSize, Dali::FittingMode::Type fittingMode,
175 Dali::SamplingMode::Type samplingMode, MaskingDataPointer& maskInfo,
176 bool synchronousLoading, TextureManager::TextureId& textureId, Vector4& textureRect,
177 Dali::ImageDimensions& textureRectSize, bool& atlasingStatus, bool& loadingStatus,
178 Dali::WrapMode::Type wrapModeU, Dali::WrapMode::Type wrapModeV, TextureUploadObserver* textureObserver,
179 AtlasUploadObserver* atlasObserver, ImageAtlasManagerPtr imageAtlasManager, bool orientationCorrection,
180 TextureManager::ReloadPolicy reloadPolicy, TextureManager::MultiplyOnLoad& preMultiplyOnLoad )
182 TextureSet textureSet;
184 loadingStatus = false;
185 textureRect = FULL_ATLAS_RECT;
187 if( VisualUrl::TEXTURE == url.GetProtocolType())
189 std::string location = url.GetLocation();
190 if( location.size() > 0u )
192 TextureId id = std::stoi( location );
193 for( auto&& elem : mExternalTextures )
195 if( elem.textureId == id )
197 preMultiplyOnLoad = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
198 textureId = elem.textureId;
199 return elem.textureSet;
204 else if( synchronousLoading )
209 Devel::PixelBuffer pixelBuffer = LoadImageFromFile( url.GetUrl(), desiredSize, fittingMode, samplingMode,
210 orientationCorrection );
213 Devel::PixelBuffer maskPixelBuffer = LoadImageFromFile( maskInfo->mAlphaMaskUrl.GetUrl(), ImageDimensions(),
214 FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, true );
215 if( maskPixelBuffer )
217 pixelBuffer.ApplyMask( maskPixelBuffer, maskInfo->mContentScaleFactor, maskInfo->mCropToMask );
222 PreMultiply( pixelBuffer, preMultiplyOnLoad );
223 data = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
229 textureSet = TextureSet::New();
230 Devel::PixelBuffer pixelBuffer = LoadImageFromFile( mBrokenImageUrl );
233 PreMultiply( pixelBuffer, preMultiplyOnLoad );
234 data = Devel::PixelBuffer::Convert(pixelBuffer); // takes ownership of buffer
236 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, data.GetPixelFormat(),
237 data.GetWidth(), data.GetHeight() );
238 texture.Upload( data );
239 textureSet = TextureSet::New();
240 textureSet.SetTexture( 0u, texture );
244 if( atlasingStatus ) // attempt atlasing
246 textureSet = imageAtlasManager->Add( textureRect, data );
248 if( !textureSet ) // big image, no atlasing or atlasing failed
250 atlasingStatus = false;
251 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, data.GetPixelFormat(),
252 data.GetWidth(), data.GetHeight() );
253 texture.Upload( data );
254 textureSet = TextureSet::New();
255 textureSet.SetTexture( 0u, texture );
259 textureRectSize.SetWidth(data.GetWidth());
260 textureRectSize.SetHeight(data.GetHeight());
266 loadingStatus = true;
269 textureSet = imageAtlasManager->Add( textureRect, url.GetUrl(), desiredSize, fittingMode, true, atlasObserver);
271 if( !textureSet ) // big image, no atlasing or atlasing failed
273 atlasingStatus = false;
276 textureId = RequestLoad( url, desiredSize, fittingMode, samplingMode, TextureManager::NO_ATLAS,
277 textureObserver, orientationCorrection, reloadPolicy, preMultiplyOnLoad );
281 maskInfo->mAlphaMaskId = RequestMaskLoad( maskInfo->mAlphaMaskUrl );
282 textureId = RequestLoad( url,
283 maskInfo->mAlphaMaskId,
284 maskInfo->mContentScaleFactor,
286 fittingMode, samplingMode,
287 TextureManager::NO_ATLAS,
288 maskInfo->mCropToMask,
290 orientationCorrection,
291 reloadPolicy, preMultiplyOnLoad );
294 TextureManager::LoadState loadState = GetTextureStateInternal( textureId );
295 if( loadState == TextureManager::UPLOADED )
297 // UploadComplete has already been called - keep the same texture set
298 textureSet = GetTextureSet( textureId );
301 // If we are loading the texture, or waiting for the ready signal handler to complete, inform
302 // caller that they need to wait.
303 loadingStatus = ( loadState == TextureManager::LOADING ||
304 loadState == TextureManager::WAITING_FOR_MASK ||
305 loadState == TextureManager::MASK_APPLYING ||
306 loadState == TextureManager::MASK_APPLIED ||
307 loadState == TextureManager::NOT_STARTED ||
313 textureRectSize = desiredSize;
317 if( ! atlasingStatus && textureSet )
319 Sampler sampler = Sampler::New();
320 sampler.SetWrapMode( wrapModeU, wrapModeV );
321 textureSet.SetSampler( 0u, sampler );
327 TextureManager::TextureId TextureManager::RequestLoad(
328 const VisualUrl& url,
329 const ImageDimensions desiredSize,
330 FittingMode::Type fittingMode,
331 Dali::SamplingMode::Type samplingMode,
332 const UseAtlas useAtlas,
333 TextureUploadObserver* observer,
334 bool orientationCorrection,
335 TextureManager::ReloadPolicy reloadPolicy,
336 TextureManager::MultiplyOnLoad& preMultiplyOnLoad )
338 return RequestLoadInternal( url, INVALID_TEXTURE_ID, 1.0f, desiredSize, fittingMode, samplingMode, useAtlas,
339 false, UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy,
340 preMultiplyOnLoad, false );
343 TextureManager::TextureId TextureManager::RequestLoad(
344 const VisualUrl& url,
345 TextureId maskTextureId,
347 const ImageDimensions desiredSize,
348 FittingMode::Type fittingMode,
349 Dali::SamplingMode::Type samplingMode,
350 const UseAtlas useAtlas,
352 TextureUploadObserver* observer,
353 bool orientationCorrection,
354 TextureManager::ReloadPolicy reloadPolicy,
355 TextureManager::MultiplyOnLoad& preMultiplyOnLoad )
357 return RequestLoadInternal( url, maskTextureId, contentScale, desiredSize, fittingMode, samplingMode, useAtlas,
358 cropToMask, UPLOAD_TO_TEXTURE, observer, orientationCorrection, reloadPolicy,
359 preMultiplyOnLoad, false );
362 TextureManager::TextureId TextureManager::RequestMaskLoad( const VisualUrl& maskUrl )
364 // Use the normal load procedure to get the alpha mask.
365 auto preMultiply = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
366 return RequestLoadInternal( maskUrl, INVALID_TEXTURE_ID, 1.0f, ImageDimensions(), FittingMode::SCALE_TO_FILL,
367 SamplingMode::NO_FILTER, NO_ATLAS, false, KEEP_PIXEL_BUFFER, NULL, true,
368 TextureManager::ReloadPolicy::CACHED, preMultiply, false );
371 TextureManager::TextureId TextureManager::RequestLoadInternal(
372 const VisualUrl& url,
373 TextureId maskTextureId,
375 const ImageDimensions desiredSize,
376 FittingMode::Type fittingMode,
377 Dali::SamplingMode::Type samplingMode,
380 StorageType storageType,
381 TextureUploadObserver* observer,
382 bool orientationCorrection,
383 TextureManager::ReloadPolicy reloadPolicy,
384 TextureManager::MultiplyOnLoad& preMultiplyOnLoad,
385 bool loadPixelBuffer )
387 // First check if the requested Texture is cached.
388 const TextureHash textureHash = GenerateHash( url.GetUrl(), desiredSize, fittingMode, samplingMode, useAtlas,
391 TextureManager::TextureId textureId = INVALID_TEXTURE_ID;
393 // Look up the texture by hash. Note: The extra parameters are used in case of a hash collision.
394 int cacheIndex = FindCachedTexture( textureHash, url.GetUrl(), desiredSize, fittingMode, samplingMode, useAtlas,
395 maskTextureId, preMultiplyOnLoad );
397 // Check if the requested Texture exists in the cache.
398 if( cacheIndex != INVALID_CACHE_INDEX )
400 if ( TextureManager::ReloadPolicy::CACHED == reloadPolicy )
402 // Mark this texture being used by another client resource. Forced reload would replace the current texture
403 // without the need for incrementing the reference count.
404 ++( mTextureInfoContainer[ cacheIndex ].referenceCount );
406 textureId = mTextureInfoContainer[ cacheIndex ].textureId;
408 // Update preMultiplyOnLoad value. It should be changed according to preMultiplied value of the cached info.
409 preMultiplyOnLoad = mTextureInfoContainer[ cacheIndex ].preMultiplied ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
411 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::General, "TextureManager::RequestLoad( url=%s observer=%p ) Using cached texture id@%d, textureId=%d\n",
412 url.GetUrl().c_str(), observer, cacheIndex, textureId );
415 if( textureId == INVALID_TEXTURE_ID ) // There was no caching, or caching not required
417 // We need a new Texture.
418 textureId = GenerateUniqueTextureId();
419 bool preMultiply = ( preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD );
420 mTextureInfoContainer.push_back( TextureInfo( textureId, maskTextureId, url.GetUrl(),
421 desiredSize, contentScale, fittingMode, samplingMode,
422 false, cropToMask, useAtlas, textureHash, orientationCorrection,
423 preMultiply, loadPixelBuffer ) );
424 cacheIndex = mTextureInfoContainer.size() - 1u;
426 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::General, "TextureManager::RequestLoad( url=%s observer=%p ) New texture, cacheIndex:%d, textureId=%d\n",
427 url.GetUrl().c_str(), observer, cacheIndex, textureId );
430 // The below code path is common whether we are using the cache or not.
431 // The textureInfoIndex now refers to either a pre-existing cached TextureInfo,
432 // or a new TextureInfo just created.
433 TextureInfo& textureInfo( mTextureInfoContainer[ cacheIndex ] );
434 textureInfo.maskTextureId = maskTextureId;
435 textureInfo.storageType = storageType;
436 textureInfo.orientationCorrection = orientationCorrection;
438 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::General, "TextureInfo loadState:%s\n",
439 GET_LOAD_STATE_STRING(textureInfo.loadState ) );
441 // Force reloading of texture by setting loadState unless already loading or cancelled.
442 if ( TextureManager::ReloadPolicy::FORCED == reloadPolicy &&
443 TextureManager::LOADING != textureInfo.loadState &&
444 TextureManager::WAITING_FOR_MASK != textureInfo.loadState &&
445 TextureManager::MASK_APPLYING != textureInfo.loadState &&
446 TextureManager::MASK_APPLIED != textureInfo.loadState &&
447 TextureManager::CANCELLED != textureInfo.loadState )
449 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Verbose, "TextureManager::RequestLoad( url=%s observer=%p ) ForcedReload cacheIndex:%d, textureId=%d\n",
450 url.GetUrl().c_str(), observer, cacheIndex, textureId );
451 textureInfo.loadState = TextureManager::NOT_STARTED;
454 // Check if we should add the observer.
455 // Only do this if we have not loaded yet and it will not have loaded by the end of this method.
456 switch( textureInfo.loadState )
458 case TextureManager::LOAD_FAILED: // Failed notifies observer which then stops observing.
459 case TextureManager::NOT_STARTED:
461 LoadOrQueueTexture( textureInfo, observer ); // If called inside NotifyObservers, queues until afterwards
464 case TextureManager::LOADING:
465 case TextureManager::WAITING_FOR_MASK:
466 case TextureManager::MASK_APPLYING:
467 case TextureManager::MASK_APPLIED:
469 ObserveTexture( textureInfo, observer );
472 case TextureManager::UPLOADED:
476 LoadOrQueueTexture( textureInfo, observer );
480 case TextureManager::CANCELLED:
482 // A cancelled texture hasn't finished loading yet. Treat as a loading texture
483 // (it's ref count has already been incremented, above)
484 textureInfo.loadState = TextureManager::LOADING;
485 ObserveTexture( textureInfo, observer );
488 case TextureManager::LOAD_FINISHED:
489 // Loading has already completed.
490 if( observer && textureInfo.loadPixelBuffer )
492 LoadOrQueueTexture( textureInfo, observer );
497 // Return the TextureId for which this Texture can now be referenced by externally.
501 void TextureManager::Remove( const TextureManager::TextureId textureId, TextureUploadObserver* observer )
503 int textureInfoIndex = GetCacheIndexFromId( textureId );
504 if( textureInfoIndex != INVALID_INDEX )
506 TextureInfo& textureInfo( mTextureInfoContainer[ textureInfoIndex ] );
508 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise,
509 "TextureManager::Remove(%d) url:%s\n cacheIdx:%d loadState:%s reference count = %d\n",
510 textureId, textureInfo.url.GetUrl().c_str(),
511 textureInfoIndex, GET_LOAD_STATE_STRING( textureInfo.loadState ), textureInfo.referenceCount );
513 // Decrement the reference count and check if this is the last user of this Texture.
514 if( --textureInfo.referenceCount <= 0 )
516 // This is the last remove for this Texture.
517 textureInfo.referenceCount = 0;
518 bool removeTextureInfo = false;
520 // If loaded, we can remove the TextureInfo and the Atlas (if atlased).
521 if( textureInfo.loadState == UPLOADED )
523 if( textureInfo.atlas )
525 textureInfo.atlas.Remove( textureInfo.atlasRect );
527 removeTextureInfo = true;
529 else if( textureInfo.loadState == LOADING )
531 // We mark the textureInfo for removal.
532 // Once the load has completed, this method will be called again.
533 textureInfo.loadState = CANCELLED;
537 // In other states, we are not waiting for a load so we are safe to remove the TextureInfo data.
538 removeTextureInfo = true;
541 // If the state allows us to remove the TextureInfo data, we do so.
542 if( removeTextureInfo )
544 // Permanently remove the textureInfo struct.
545 mTextureInfoContainer.erase( mTextureInfoContainer.begin() + textureInfoIndex );
551 // Remove element from the LoadQueue
552 for( auto&& element : mLoadQueue )
554 if( element.mObserver == observer )
556 mLoadQueue.Erase( &element );
564 VisualUrl TextureManager::GetVisualUrl( TextureId textureId )
566 VisualUrl visualUrl("");
567 int cacheIndex = GetCacheIndexFromId( textureId );
569 if( cacheIndex != INVALID_CACHE_INDEX )
571 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::GetVisualUrl. Using cached texture id=%d, textureId=%d\n",
572 cacheIndex, textureId );
574 TextureInfo& cachedTextureInfo( mTextureInfoContainer[ cacheIndex ] );
575 visualUrl = cachedTextureInfo.url;
580 TextureManager::LoadState TextureManager::GetTextureState( TextureId textureId )
582 LoadState loadState = TextureManager::NOT_STARTED;
584 int cacheIndex = GetCacheIndexFromId( textureId );
585 if( cacheIndex != INVALID_CACHE_INDEX )
587 TextureInfo& cachedTextureInfo( mTextureInfoContainer[ cacheIndex ] );
588 loadState = cachedTextureInfo.loadState;
592 for( auto&& elem : mExternalTextures )
594 if( elem.textureId == textureId )
596 loadState = LoadState::UPLOADED;
604 TextureManager::LoadState TextureManager::GetTextureStateInternal( TextureId textureId )
606 LoadState loadState = TextureManager::NOT_STARTED;
608 int cacheIndex = GetCacheIndexFromId( textureId );
609 if( cacheIndex != INVALID_CACHE_INDEX )
611 TextureInfo& cachedTextureInfo( mTextureInfoContainer[ cacheIndex ] );
612 loadState = cachedTextureInfo.loadState;
618 TextureSet TextureManager::GetTextureSet( TextureId textureId )
620 TextureSet textureSet;// empty handle
622 int cacheIndex = GetCacheIndexFromId( textureId );
623 if( cacheIndex != INVALID_CACHE_INDEX )
625 TextureInfo& cachedTextureInfo( mTextureInfoContainer[ cacheIndex ] );
626 textureSet = cachedTextureInfo.textureSet;
630 for( auto&& elem : mExternalTextures )
632 if( elem.textureId == textureId )
634 textureSet = elem.textureSet;
642 std::string TextureManager::AddExternalTexture( TextureSet& textureSet )
644 TextureManager::ExternalTextureInfo info;
645 info.textureId = GenerateUniqueTextureId();
646 info.textureSet = textureSet;
647 mExternalTextures.emplace_back( info );
648 return VisualUrl::CreateTextureUrl( std::to_string( info.textureId ) );
651 TextureSet TextureManager::RemoveExternalTexture( const std::string& url )
653 if( url.size() > 0u )
655 // get the location from the Url
656 VisualUrl parseUrl( url );
657 if( VisualUrl::TEXTURE == parseUrl.GetProtocolType() )
659 std::string location = parseUrl.GetLocation();
660 if( location.size() > 0u )
662 TextureId id = std::stoi( location );
663 const auto end = mExternalTextures.end();
664 for( auto iter = mExternalTextures.begin(); iter != end; ++iter )
666 if( iter->textureId == id )
668 auto textureSet = iter->textureSet;
669 mExternalTextures.erase( iter );
680 void TextureManager::AddObserver( TextureManager::LifecycleObserver& observer )
682 // make sure an observer doesn't observe the same object twice
683 // otherwise it will get multiple calls to ObjectDestroyed()
684 DALI_ASSERT_DEBUG( mLifecycleObservers.End() == std::find( mLifecycleObservers.Begin(), mLifecycleObservers.End(), &observer));
685 mLifecycleObservers.PushBack( &observer );
688 void TextureManager::RemoveObserver( TextureManager::LifecycleObserver& observer)
690 // Find the observer...
691 auto endIter = mLifecycleObservers.End();
692 for( auto iter = mLifecycleObservers.Begin(); iter != endIter; ++iter)
694 if( (*iter) == &observer)
696 mLifecycleObservers.Erase( iter );
700 DALI_ASSERT_DEBUG(endIter != mLifecycleObservers.End());
703 void TextureManager::LoadOrQueueTexture( TextureInfo& textureInfo, TextureUploadObserver* observer )
705 switch( textureInfo.loadState )
712 QueueLoadTexture( textureInfo, observer );
716 LoadTexture( textureInfo, observer );
724 QueueLoadTexture( textureInfo, observer );
728 // The Texture has already loaded. The other observers have already been notified.
729 // We need to send a "late" loaded notification for this observer.
730 observer->UploadComplete( true, textureInfo.textureId, textureInfo.textureSet,
731 textureInfo.useAtlas, textureInfo.atlasRect,
732 textureInfo.preMultiplied );
739 case WAITING_FOR_MASK:
748 void TextureManager::QueueLoadTexture( TextureInfo& textureInfo, TextureUploadObserver* observer )
750 auto textureId = textureInfo.textureId;
751 mLoadQueue.PushBack( LoadQueueElement( textureId, observer) );
754 void TextureManager::LoadTexture( TextureInfo& textureInfo, TextureUploadObserver* observer )
756 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::LoadTexture(): url:%s sync:%s\n",
757 textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously?"T":"F" );
759 textureInfo.loadState = LOADING;
760 if( !textureInfo.loadSynchronously )
762 auto& loadersContainer = textureInfo.url.IsLocalResource() ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
763 auto loadingHelperIt = loadersContainer.GetNext();
764 auto premultiplyOnLoad = ( textureInfo.preMultiplyOnLoad && textureInfo.maskTextureId == INVALID_TEXTURE_ID ) ?
765 DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
766 DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
767 loadingHelperIt->Load(textureInfo.textureId, textureInfo.url,
768 textureInfo.desiredSize, textureInfo.fittingMode,
769 textureInfo.samplingMode, textureInfo.orientationCorrection,
772 ObserveTexture( textureInfo, observer );
775 void TextureManager::ProcessQueuedTextures()
777 for( auto&& element : mLoadQueue )
779 int cacheIndex = GetCacheIndexFromId( element.mTextureId );
780 if( cacheIndex != INVALID_CACHE_INDEX )
782 TextureInfo& textureInfo( mTextureInfoContainer[cacheIndex] );
783 if( textureInfo.loadState == UPLOADED )
785 element.mObserver->UploadComplete( true, textureInfo.textureId, textureInfo.textureSet,
786 textureInfo.useAtlas, textureInfo.atlasRect,
787 textureInfo.preMultiplied );
789 else if ( textureInfo.loadState == LOAD_FINISHED && textureInfo.loadPixelBuffer )
791 element.mObserver->LoadComplete( true, textureInfo.pixelBuffer, textureInfo.url, textureInfo.preMultiplied );
795 LoadTexture( textureInfo, element.mObserver );
802 void TextureManager::ObserveTexture( TextureInfo& textureInfo,
803 TextureUploadObserver* observer )
805 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::ObserveTexture(): url:%s observer:%p\n",
806 textureInfo.url.GetUrl().c_str(), observer );
810 textureInfo.observerList.PushBack( observer );
811 observer->DestructionSignal().Connect( this, &TextureManager::ObserverDestroyed );
815 void TextureManager::AsyncLoadComplete( AsyncLoadingInfoContainerType& loadingContainer, uint32_t id,
816 Devel::PixelBuffer pixelBuffer )
818 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::AsyncLoadComplete( id:%d )\n", id );
820 if( loadingContainer.size() >= 1u )
822 AsyncLoadingInfo loadingInfo = loadingContainer.front();
824 if( loadingInfo.loadId == id )
826 int cacheIndex = GetCacheIndexFromId( loadingInfo.textureId );
827 if( cacheIndex != INVALID_CACHE_INDEX )
829 TextureInfo& textureInfo( mTextureInfoContainer[cacheIndex] );
831 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise,
832 " textureId:%d Url:%s CacheIndex:%d LoadState: %d\n",
833 textureInfo.textureId, textureInfo.url.GetUrl().c_str(), cacheIndex, textureInfo.loadState );
835 if( textureInfo.loadState != CANCELLED )
837 // textureInfo can be invalidated after this call (as the mTextureInfoContainer may be modified)
838 PostLoad( textureInfo, pixelBuffer );
842 Remove( textureInfo.textureId, nullptr );
847 loadingContainer.pop_front();
851 void TextureManager::PostLoad( TextureInfo& textureInfo, Devel::PixelBuffer& pixelBuffer )
853 // Was the load successful?
854 if( pixelBuffer && ( pixelBuffer.GetWidth() != 0 ) && ( pixelBuffer.GetHeight() != 0 ) )
856 // No atlas support for now
857 textureInfo.useAtlas = NO_ATLAS;
858 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
860 if( textureInfo.storageType == UPLOAD_TO_TEXTURE )
862 // If there is a mask texture ID associated with this texture, then apply the mask
863 // if it's already loaded. If it hasn't, and the mask is still loading,
864 // wait for the mask to finish loading.
865 if( textureInfo.maskTextureId != INVALID_TEXTURE_ID )
867 if( textureInfo.loadState == MASK_APPLYING )
869 textureInfo.loadState = MASK_APPLIED;
870 UploadTexture( pixelBuffer, textureInfo );
871 NotifyObservers( textureInfo, true );
875 LoadState maskLoadState = GetTextureStateInternal( textureInfo.maskTextureId );
876 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily
877 if( maskLoadState == LOADING )
879 textureInfo.loadState = WAITING_FOR_MASK;
881 else if( maskLoadState == LOAD_FINISHED )
883 // Send New Task to Thread
884 ApplyMask( textureInfo, textureInfo.maskTextureId );
890 UploadTexture( pixelBuffer, textureInfo );
891 NotifyObservers( textureInfo, true );
896 textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data
897 textureInfo.loadState = LOAD_FINISHED;
899 if( textureInfo.loadPixelBuffer )
901 NotifyObservers( textureInfo, true );
903 // Check if there was another texture waiting for this load to complete
904 // (e.g. if this was an image mask, and its load is on a different thread)
905 CheckForWaitingTexture( textureInfo );
910 // @todo If the load was unsuccessful, upload the broken image.
911 textureInfo.loadState = LOAD_FAILED;
912 CheckForWaitingTexture( textureInfo );
913 NotifyObservers( textureInfo, false );
917 void TextureManager::CheckForWaitingTexture( TextureInfo& maskTextureInfo )
919 // Search the cache, checking if any texture has this texture id as a
921 const unsigned int size = mTextureInfoContainer.size();
923 for( unsigned int cacheIndex = 0; cacheIndex < size; ++cacheIndex )
925 if( mTextureInfoContainer[cacheIndex].maskTextureId == maskTextureInfo.textureId &&
926 mTextureInfoContainer[cacheIndex].loadState == WAITING_FOR_MASK )
928 TextureInfo& textureInfo( mTextureInfoContainer[cacheIndex] );
930 if( maskTextureInfo.loadState == LOAD_FINISHED )
932 // Send New Task to Thread
933 ApplyMask( textureInfo, maskTextureInfo.textureId );
937 textureInfo.pixelBuffer.Reset();
938 textureInfo.loadState = LOAD_FAILED;
939 NotifyObservers( textureInfo, false );
945 void TextureManager::ApplyMask( TextureInfo& textureInfo, TextureId maskTextureId )
947 int maskCacheIndex = GetCacheIndexFromId( maskTextureId );
948 if( maskCacheIndex != INVALID_CACHE_INDEX )
950 Devel::PixelBuffer maskPixelBuffer = mTextureInfoContainer[maskCacheIndex].pixelBuffer;
951 Devel::PixelBuffer pixelBuffer = textureInfo.pixelBuffer;
952 textureInfo.pixelBuffer.Reset();
954 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::ApplyMask(): url:%s sync:%s\n",
955 textureInfo.url.GetUrl().c_str(), textureInfo.loadSynchronously?"T":"F" );
957 textureInfo.loadState = MASK_APPLYING;
958 auto& loadersContainer = textureInfo.url.IsLocalResource() ? mAsyncLocalLoaders : mAsyncRemoteLoaders;
959 auto loadingHelperIt = loadersContainer.GetNext();
960 auto premultiplyOnLoad = textureInfo.preMultiplyOnLoad ? DevelAsyncImageLoader::PreMultiplyOnLoad::ON : DevelAsyncImageLoader::PreMultiplyOnLoad::OFF;
961 DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End());
962 loadingHelperIt->ApplyMask( textureInfo.textureId, pixelBuffer, maskPixelBuffer, textureInfo.scaleFactor, textureInfo.cropToMask, premultiplyOnLoad );
966 void TextureManager::UploadTexture( Devel::PixelBuffer& pixelBuffer, TextureInfo& textureInfo )
968 if( textureInfo.useAtlas != USE_ATLAS )
970 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::General, " TextureManager::UploadTexture() New Texture for textureId:%d\n", textureInfo.textureId );
972 // Check if this pixelBuffer is premultiplied
973 textureInfo.preMultiplied = pixelBuffer.IsAlphaPreMultiplied();
975 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(),
976 pixelBuffer.GetWidth(), pixelBuffer.GetHeight() );
978 PixelData pixelData = Devel::PixelBuffer::Convert( pixelBuffer );
979 texture.Upload( pixelData );
980 if ( ! textureInfo.textureSet )
982 textureInfo.textureSet = TextureSet::New();
984 textureInfo.textureSet.SetTexture( 0u, texture );
987 // Update the load state.
988 // Note: This is regardless of success as we care about whether a
989 // load attempt is in progress or not. If unsuccessful, a broken
990 // image is still loaded.
991 textureInfo.loadState = UPLOADED;
994 void TextureManager::NotifyObservers( TextureInfo& textureInfo, bool success )
996 TextureId textureId = textureInfo.textureId;
998 // If there is an observer: Notify the load is complete, whether successful or not,
999 // and erase it from the list
1000 TextureInfo* info = &textureInfo;
1002 mQueueLoadFlag = true;
1004 while( info->observerList.Count() )
1006 TextureUploadObserver* observer = info->observerList[0];
1008 // During UploadComplete() a Control ResourceReady() signal is emitted.
1009 // During that signal the app may add remove /add Textures (e.g. via
1011 // It is possible for observers to be removed from the observer list,
1012 // and it is also possible for the mTextureInfoContainer to be modified,
1013 // invalidating the reference to the textureInfo struct.
1014 // Texture load requests for the same URL are deferred until the end of this
1016 DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "NotifyObservers() url:%s loadState:%s\n",
1017 textureInfo.url.GetUrl().c_str(), GET_LOAD_STATE_STRING(textureInfo.loadState ) );
1019 if( info->loadPixelBuffer )
1021 observer->LoadComplete( success, info->pixelBuffer, info->url, info->preMultiplied );
1025 observer->UploadComplete( success, info->textureId, info->textureSet, info->useAtlas, info->atlasRect,
1026 info->preMultiplied );
1029 observer->DestructionSignal().Disconnect( this, &TextureManager::ObserverDestroyed );
1031 // Get the textureInfo from the container again as it may have been invalidated.
1032 int textureInfoIndex = GetCacheIndexFromId( textureId );
1033 if( textureInfoIndex == INVALID_CACHE_INDEX)
1035 break; // texture has been removed - can stop.
1037 info = &mTextureInfoContainer[ textureInfoIndex ];
1039 // remove the observer that was just triggered if it's still in the list
1040 for( TextureInfo::ObserverListType::Iterator j = info->observerList.Begin(); j != info->observerList.End(); ++j )
1042 if( *j == observer )
1044 info->observerList.Erase( j );
1050 mQueueLoadFlag = false;
1051 ProcessQueuedTextures();
1053 if( info->loadPixelBuffer && info->observerList.Count() == 0 )
1055 Remove( info->textureId, nullptr );
1059 TextureManager::TextureId TextureManager::GenerateUniqueTextureId()
1061 return mCurrentTextureId++;
1064 int TextureManager::GetCacheIndexFromId( const TextureId textureId )
1066 const unsigned int size = mTextureInfoContainer.size();
1068 for( unsigned int i = 0; i < size; ++i )
1070 if( mTextureInfoContainer[i].textureId == textureId )
1076 return INVALID_CACHE_INDEX;
1079 TextureManager::TextureHash TextureManager::GenerateHash(
1080 const std::string& url,
1081 const ImageDimensions size,
1082 const FittingMode::Type fittingMode,
1083 const Dali::SamplingMode::Type samplingMode,
1084 const UseAtlas useAtlas,
1085 TextureId maskTextureId )
1087 std::string hashTarget( url );
1088 const size_t urlLength = hashTarget.length();
1089 const uint16_t width = size.GetWidth();
1090 const uint16_t height = size.GetWidth();
1092 // If either the width or height has been specified, include the resizing options in the hash
1093 if( width != 0 || height != 0 )
1095 // We are appending 5 bytes to the URL to form the hash input.
1096 hashTarget.resize( urlLength + 5u );
1097 char* hashTargetPtr = &( hashTarget[ urlLength ] );
1099 // Pack the width and height (4 bytes total).
1100 *hashTargetPtr++ = size.GetWidth() & 0xff;
1101 *hashTargetPtr++ = ( size.GetWidth() >> 8u ) & 0xff;
1102 *hashTargetPtr++ = size.GetHeight() & 0xff;
1103 *hashTargetPtr++ = ( size.GetHeight() >> 8u ) & 0xff;
1105 // Bit-pack the FittingMode, SamplingMode and atlasing.
1106 // FittingMode=2bits, SamplingMode=3bits, useAtlas=1bit
1107 *hashTargetPtr = ( fittingMode << 4u ) | ( samplingMode << 1 ) | useAtlas;
1111 // We are not including sizing information, but we still need an extra byte for atlasing.
1112 hashTarget.resize( urlLength + 1u );
1114 // Add the atlasing to the hash input.
1117 case UseAtlas::NO_ATLAS:
1119 hashTarget[ urlLength ] = 'f';
1122 case UseAtlas::USE_ATLAS:
1124 hashTarget[ urlLength ] = 't';
1130 if( maskTextureId != INVALID_TEXTURE_ID )
1132 auto textureIdIndex = hashTarget.length();
1133 hashTarget.resize( hashTarget.length() + sizeof( TextureId ) );
1134 unsigned char* hashTargetPtr = reinterpret_cast<unsigned char*>(&( hashTarget[ textureIdIndex ] ));
1136 // Append the texture id to the end of the URL byte by byte:
1137 // (to avoid SIGBUS / alignment issues)
1138 for( size_t byteIter = 0; byteIter < sizeof( TextureId ); ++byteIter )
1140 *hashTargetPtr++ = maskTextureId & 0xff;
1141 maskTextureId >>= 8u;
1145 return Dali::CalculateHash( hashTarget );
1148 int TextureManager::FindCachedTexture(
1149 const TextureManager::TextureHash hash,
1150 const std::string& url,
1151 const ImageDimensions size,
1152 const FittingMode::Type fittingMode,
1153 const Dali::SamplingMode::Type samplingMode,
1154 const bool useAtlas,
1155 TextureId maskTextureId,
1156 TextureManager::MultiplyOnLoad preMultiplyOnLoad )
1158 // Default to an invalid ID, in case we do not find a match.
1159 int cacheIndex = INVALID_CACHE_INDEX;
1161 // Iterate through our hashes to find a match.
1162 const unsigned int count = mTextureInfoContainer.size();
1163 for( unsigned int i = 0u; i < count; ++i )
1165 if( mTextureInfoContainer[i].hash == hash )
1167 // We have a match, now we check all the original parameters in case of a hash collision.
1168 TextureInfo& textureInfo( mTextureInfoContainer[i] );
1170 if( ( url == textureInfo.url.GetUrl() ) &&
1171 ( useAtlas == textureInfo.useAtlas ) &&
1172 ( maskTextureId == textureInfo.maskTextureId ) &&
1173 ( size == textureInfo.desiredSize ) &&
1174 ( ( size.GetWidth() == 0 && size.GetHeight() == 0 ) ||
1175 ( fittingMode == textureInfo.fittingMode &&
1176 samplingMode == textureInfo.samplingMode ) ) )
1178 // 1. If preMultiplyOnLoad is MULTIPLY_ON_LOAD, then textureInfo.preMultiplyOnLoad should be true. The premultiplication result can be different.
1179 // 2. If preMultiplyOnLoad is LOAD_WITHOUT_MULTIPLY, then textureInfo.preMultiplied should be false.
1180 if( ( preMultiplyOnLoad == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD && textureInfo.preMultiplyOnLoad )
1181 || ( preMultiplyOnLoad == TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY && !textureInfo.preMultiplied ) )
1183 // The found Texture is a match.
1194 void TextureManager::ObserverDestroyed( TextureUploadObserver* observer )
1196 const unsigned int count = mTextureInfoContainer.size();
1197 for( unsigned int i = 0; i < count; ++i )
1199 TextureInfo& textureInfo( mTextureInfoContainer[i] );
1200 for( TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin();
1201 j != textureInfo.observerList.End(); )
1203 if( *j == observer )
1205 j = textureInfo.observerList.Erase( j );
1216 TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(TextureManager& textureManager)
1217 : AsyncLoadingHelper(Toolkit::AsyncImageLoader::New(), textureManager,
1218 AsyncLoadingInfoContainerType())
1222 void TextureManager::AsyncLoadingHelper::Load(TextureId textureId,
1223 const VisualUrl& url,
1224 ImageDimensions desiredSize,
1225 FittingMode::Type fittingMode,
1226 SamplingMode::Type samplingMode,
1227 bool orientationCorrection,
1228 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad)
1230 mLoadingInfoContainer.push_back(AsyncLoadingInfo(textureId));
1231 auto id = DevelAsyncImageLoader::Load( mLoader, url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection, preMultiplyOnLoad );
1232 mLoadingInfoContainer.back().loadId = id;
1235 void TextureManager::AsyncLoadingHelper::ApplyMask( TextureId textureId,
1236 Devel::PixelBuffer pixelBuffer,
1237 Devel::PixelBuffer maskPixelBuffer,
1240 DevelAsyncImageLoader::PreMultiplyOnLoad preMultiplyOnLoad )
1242 mLoadingInfoContainer.push_back(AsyncLoadingInfo(textureId));
1243 auto id = DevelAsyncImageLoader::ApplyMask( mLoader, pixelBuffer, maskPixelBuffer, contentScale, cropToMask, preMultiplyOnLoad );
1244 mLoadingInfoContainer.back().loadId = id;
1247 TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(AsyncLoadingHelper&& rhs)
1248 : AsyncLoadingHelper(rhs.mLoader, rhs.mTextureManager, std::move(rhs.mLoadingInfoContainer))
1252 TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(
1253 Toolkit::AsyncImageLoader loader,
1254 TextureManager& textureManager,
1255 AsyncLoadingInfoContainerType&& loadingInfoContainer)
1257 mTextureManager(textureManager),
1258 mLoadingInfoContainer(std::move(loadingInfoContainer))
1260 DevelAsyncImageLoader::PixelBufferLoadedSignal(mLoader).Connect(
1261 this, &AsyncLoadingHelper::AsyncLoadComplete);
1264 void TextureManager::AsyncLoadingHelper::AsyncLoadComplete(uint32_t id,
1265 Devel::PixelBuffer pixelBuffer )
1267 mTextureManager.AsyncLoadComplete( mLoadingInfoContainer, id, pixelBuffer );
1270 void TextureManager::SetBrokenImageUrl(const std::string& brokenImageUrl)
1272 mBrokenImageUrl = brokenImageUrl;
1275 } // namespace Internal
1277 } // namespace Toolkit