X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fvisuals%2Ftexture-manager.cpp;h=9468935c556d47be2d4984d3eb9dab2278c040ab;hp=2283f220251d02d986c435d8be0b5212dd66b637;hb=421e8bf2b19f6e6e53866595d29db9cc6b8d210e;hpb=62420f77af61e162a10fdb5d0f8e129d2bdaca97 diff --git a/dali-toolkit/internal/visuals/texture-manager.cpp b/dali-toolkit/internal/visuals/texture-manager.cpp index 2283f22..9468935 100644 --- a/dali-toolkit/internal/visuals/texture-manager.cpp +++ b/dali-toolkit/internal/visuals/texture-manager.cpp @@ -19,17 +19,45 @@ #include "texture-manager.h" // EXTERNAL HEADERS +#include +#include #include -#include #include +#include #include // INTERNAL HEADERS -#include -#include #include #include +namespace +{ + +constexpr auto DEFAULT_NUMBER_OF_LOCAL_LOADER_THREADS = size_t{4u}; +constexpr auto DEFAULT_NUMBER_OF_REMOTE_LOADER_THREADS = size_t{8u}; + +constexpr auto NUMBER_OF_LOCAL_LOADER_THREADS_ENV = "DALI_TEXTURE_LOCAL_THREADS"; +constexpr auto NUMBER_OF_REMOTE_LOADER_THREADS_ENV = "DALI_TEXTURE_REMOTE_THREADS"; + +size_t GetNumberOfThreads(const char* environmentVariable, size_t defaultValue) +{ + using Dali::EnvironmentVariable::GetEnvironmentVariable; + auto numberString = GetEnvironmentVariable(environmentVariable); + auto numberOfThreads = numberString ? std::strtol(numberString, nullptr, 10) : 0; + return (numberOfThreads > 0) ? numberOfThreads : defaultValue; +} + +size_t GetNumberOfLocalLoaderThreads() +{ + return GetNumberOfThreads(NUMBER_OF_LOCAL_LOADER_THREADS_ENV, DEFAULT_NUMBER_OF_LOCAL_LOADER_THREADS); +} + +size_t GetNumberOfRemoteLoaderThreads() +{ + return GetNumberOfThreads(NUMBER_OF_REMOTE_LOADER_THREADS_ENV, DEFAULT_NUMBER_OF_REMOTE_LOADER_THREADS); +} + +} // namespace namespace Dali { @@ -57,12 +85,10 @@ const int INVALID_CACHE_INDEX( -1 ); ///< Invalid Cache index TextureManager::TextureManager() -: mAsyncLocalLoader( Toolkit::AsyncImageLoader::New() ), - mAsyncRemoteLoader( Toolkit::AsyncImageLoader::New() ), - mCurrentTextureId( 0 ) +: mCurrentTextureId( 0 ), + mAsyncLocalLoaders( GetNumberOfLocalLoaderThreads(), [&]() { return AsyncLoadingHelper(*this); } ), + mAsyncRemoteLoaders( GetNumberOfRemoteLoaderThreads(), [&]() { return AsyncLoadingHelper(*this); } ) { - mAsyncLocalLoader.ImageLoadedSignal().Connect( this, &TextureManager::AsyncLocalLoadComplete ); - mAsyncRemoteLoader.ImageLoadedSignal().Connect( this, &TextureManager::AsyncRemoteLoadComplete ); } TextureManager::TextureId TextureManager::RequestLoad( @@ -73,35 +99,39 @@ TextureManager::TextureId TextureManager::RequestLoad( const UseAtlas useAtlas, TextureUploadObserver* observer ) { - return RequestInternalLoad( url, INVALID_TEXTURE_ID, desiredSize, fittingMode, samplingMode, useAtlas, GPU_UPLOAD, observer ); + return RequestLoadInternal( url, INVALID_TEXTURE_ID, 1.0f, desiredSize, fittingMode, samplingMode, useAtlas, false, UPLOAD_TO_TEXTURE, observer ); } TextureManager::TextureId TextureManager::RequestLoad( const VisualUrl& url, TextureId maskTextureId, + float contentScale, const ImageDimensions desiredSize, FittingMode::Type fittingMode, Dali::SamplingMode::Type samplingMode, const UseAtlas useAtlas, + bool cropToMask, TextureUploadObserver* observer ) { - return RequestInternalLoad( url, maskTextureId, desiredSize, fittingMode, samplingMode, useAtlas, GPU_UPLOAD, observer ); + return RequestLoadInternal( url, maskTextureId, contentScale, desiredSize, fittingMode, samplingMode, useAtlas, cropToMask, UPLOAD_TO_TEXTURE, observer ); } TextureManager::TextureId TextureManager::RequestMaskLoad( const VisualUrl& maskUrl ) { // Use the normal load procedure to get the alpha mask. - return RequestInternalLoad( maskUrl, INVALID_TEXTURE_ID, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, NO_ATLAS, CPU, NULL ); + return RequestLoadInternal( maskUrl, INVALID_TEXTURE_ID, 1.0f, ImageDimensions(), FittingMode::SCALE_TO_FILL, SamplingMode::NO_FILTER, NO_ATLAS, false, KEEP_PIXEL_BUFFER, NULL ); } -TextureManager::TextureId TextureManager::RequestInternalLoad( +TextureManager::TextureId TextureManager::RequestLoadInternal( const VisualUrl& url, TextureId maskTextureId, + float contentScale, const ImageDimensions desiredSize, FittingMode::Type fittingMode, Dali::SamplingMode::Type samplingMode, UseAtlas useAtlas, + bool cropToMask, StorageType storageType, TextureUploadObserver* observer ) { @@ -128,8 +158,8 @@ TextureManager::TextureId TextureManager::RequestInternalLoad( // We need a new Texture. textureId = GenerateUniqueTextureId(); mTextureInfoContainer.push_back( TextureInfo( textureId, maskTextureId, url.GetUrl(), - desiredSize, fittingMode, samplingMode, - false, useAtlas, textureHash ) ); + desiredSize, contentScale, fittingMode, samplingMode, + false, cropToMask, useAtlas, textureHash ) ); cacheIndex = mTextureInfoContainer.size() - 1u; DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::RequestLoad( url=%s observer=%p ) New texture, cacheIndex:%d, textureId=%d\n", url.GetUrl().c_str(), observer, cacheIndex, textureId ); @@ -168,9 +198,8 @@ TextureManager::TextureId TextureManager::RequestInternalLoad( { // The Texture has already loaded. The other observers have already been notified. // We need to send a "late" loaded notification for this observer. - observer->UploadComplete( true, - textureInfo.textureSet, textureInfo.useAtlas, - textureInfo.atlasRect ); + observer->UploadComplete( true, textureInfo.textureId, textureInfo.textureSet, + textureInfo.useAtlas, textureInfo.atlasRect ); } break; } @@ -200,7 +229,6 @@ void TextureManager::Remove( const TextureManager::TextureId textureId ) { TextureInfo& textureInfo( mTextureInfoContainer[ textureInfoIndex ] ); - DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::Remove(%d) cacheIdx:%d loadState:%s\n", textureId, textureInfoIndex, textureInfo.loadState == TextureManager::NOT_STARTED ? "NOT_STARTED" : @@ -246,6 +274,15 @@ void TextureManager::Remove( const TextureManager::TextureId textureId ) } } +const VisualUrl& TextureManager::GetVisualUrl( TextureId textureId ) +{ + int cacheIndex = GetCacheIndexFromId( textureId ); + DALI_ASSERT_DEBUG( cacheIndex != INVALID_CACHE_INDEX && "TextureId out of range"); + + TextureInfo& cachedTextureInfo( mTextureInfoContainer[ cacheIndex ] ); + return cachedTextureInfo.url; +} + TextureManager::LoadState TextureManager::GetTextureState( TextureId textureId ) { LoadState loadState = TextureManager::NOT_STARTED; @@ -282,22 +319,12 @@ bool TextureManager::LoadTexture( TextureInfo& textureInfo ) if( !textureInfo.loadSynchronously ) { - if( textureInfo.url.IsLocal() ) - { - mAsyncLocalLoadingInfoContainer.push_back( AsyncLoadingInfo( textureInfo.textureId ) ); - mAsyncLocalLoadingInfoContainer.back().loadId = - GetImplementation(mAsyncLocalLoader).Load( textureInfo.url, textureInfo.desiredSize, - textureInfo.fittingMode, - textureInfo.samplingMode, true ); - } - else - { - mAsyncRemoteLoadingInfoContainer.push_back( AsyncLoadingInfo( textureInfo.textureId ) ); - mAsyncRemoteLoadingInfoContainer.back().loadId = - GetImplementation(mAsyncRemoteLoader).Load( textureInfo.url, textureInfo.desiredSize, - textureInfo.fittingMode, - textureInfo.samplingMode, true ); - } + auto& loadersContainer = textureInfo.url.IsLocal() ? mAsyncLocalLoaders : mAsyncRemoteLoaders; + auto loadingHelperIt = loadersContainer.GetNext(); + DALI_ASSERT_ALWAYS(loadingHelperIt != loadersContainer.End()); + loadingHelperIt->Load(textureInfo.textureId, textureInfo.url, + textureInfo.desiredSize, textureInfo.fittingMode, + textureInfo.samplingMode, true); } } @@ -314,17 +341,7 @@ void TextureManager::ObserveTexture( TextureInfo& textureInfo, } } -void TextureManager::AsyncLocalLoadComplete( uint32_t id, PixelData pixelData ) -{ - AsyncLoadComplete( mAsyncLocalLoadingInfoContainer, id, pixelData ); -} - -void TextureManager::AsyncRemoteLoadComplete( uint32_t id, PixelData pixelData ) -{ - AsyncLoadComplete( mAsyncRemoteLoadingInfoContainer, id, pixelData ); -} - -void TextureManager::AsyncLoadComplete( AsyncLoadingInfoContainerType& loadingContainer, uint32_t id, PixelData pixelData ) +void TextureManager::AsyncLoadComplete( AsyncLoadingInfoContainerType& loadingContainer, uint32_t id, Devel::PixelBuffer pixelBuffer ) { DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, "TextureManager::AsyncLoadComplete( id:%d )\n", id ); @@ -344,7 +361,7 @@ void TextureManager::AsyncLoadComplete( AsyncLoadingInfoContainerType& loadingCo if( textureInfo.loadState != CANCELLED ) { // textureInfo can be invalidated after this call (as the mTextureInfoContainer may be modified) - PostLoad( textureInfo, pixelData ); + PostLoad( textureInfo, pixelBuffer ); } else { @@ -357,15 +374,15 @@ void TextureManager::AsyncLoadComplete( AsyncLoadingInfoContainerType& loadingCo } } -void TextureManager::PostLoad( TextureInfo& textureInfo, PixelData pixelData ) +void TextureManager::PostLoad( TextureInfo& textureInfo, Devel::PixelBuffer& pixelBuffer ) { // Was the load successful? - if( pixelData && ( pixelData.GetWidth() != 0 ) && ( pixelData.GetHeight() != 0 ) ) + if( pixelBuffer && ( pixelBuffer.GetWidth() != 0 ) && ( pixelBuffer.GetHeight() != 0 ) ) { // No atlas support for now textureInfo.useAtlas = NO_ATLAS; - if( textureInfo.storageType == GPU_UPLOAD ) + if( textureInfo.storageType == UPLOAD_TO_TEXTURE ) { // If there is a mask texture ID associated with this texture, then apply the mask // if it's already loaded. If it hasn't, and the mask is still loading, @@ -375,25 +392,25 @@ void TextureManager::PostLoad( TextureInfo& textureInfo, PixelData pixelData ) LoadState maskLoadState = GetTextureState( textureInfo.maskTextureId ); if( maskLoadState == LOADING ) { - textureInfo.pixelData = pixelData; // Store the pixel data temporarily + textureInfo.pixelBuffer = pixelBuffer; // Store the pixel buffer temporarily textureInfo.loadState = WAITING_FOR_MASK; } else if( maskLoadState == LOAD_FINISHED ) { - ApplyMask( pixelData, textureInfo.maskTextureId ); - UploadTexture( pixelData, textureInfo ); + ApplyMask( pixelBuffer, textureInfo.maskTextureId, textureInfo.scaleFactor, textureInfo.cropToMask ); + UploadTexture( pixelBuffer, textureInfo ); NotifyObservers( textureInfo, true ); } } else { - UploadTexture( pixelData, textureInfo ); + UploadTexture( pixelBuffer, textureInfo ); NotifyObservers( textureInfo, true ); } } - else // currently, CPU textures are local to texture manager + else { - textureInfo.pixelData = pixelData; // Store the pixel data + textureInfo.pixelBuffer = pixelBuffer; // Store the pixel data textureInfo.loadState = LOAD_FINISHED; // Check if there was another texture waiting for this load to complete @@ -423,13 +440,13 @@ void TextureManager::CheckForWaitingTexture( TextureInfo& maskTextureInfo ) mTextureInfoContainer[cacheIndex].loadState == WAITING_FOR_MASK ) { TextureInfo& textureInfo( mTextureInfoContainer[cacheIndex] ); - PixelData pixelData = textureInfo.pixelData; - textureInfo.pixelData.Reset(); + Devel::PixelBuffer pixelBuffer = textureInfo.pixelBuffer; + textureInfo.pixelBuffer.Reset(); if( maskTextureInfo.loadState == LOAD_FINISHED ) { - ApplyMask( pixelData, maskTextureInfo.textureId ); - UploadTexture( pixelData, textureInfo ); + ApplyMask( pixelBuffer, maskTextureInfo.textureId, textureInfo.scaleFactor, textureInfo.cropToMask ); + UploadTexture( pixelBuffer, textureInfo ); NotifyObservers( textureInfo, true ); } else @@ -442,20 +459,23 @@ void TextureManager::CheckForWaitingTexture( TextureInfo& maskTextureInfo ) } } -void TextureManager::ApplyMask( PixelData pixelData, TextureId maskTextureId ) +void TextureManager::ApplyMask( + Devel::PixelBuffer& pixelBuffer, TextureId maskTextureId, + float contentScale, bool cropToMask ) { int maskCacheIndex = GetCacheIndexFromId( maskTextureId ); - PixelData maskPixelData = mTextureInfoContainer[maskCacheIndex].pixelData; - Dali::ApplyMask( pixelData, maskPixelData ); + Devel::PixelBuffer maskPixelBuffer = mTextureInfoContainer[maskCacheIndex].pixelBuffer; + pixelBuffer.ApplyMask( maskPixelBuffer, contentScale, cropToMask ); } -void TextureManager::UploadTexture( PixelData pixelData, TextureInfo& textureInfo ) +void TextureManager::UploadTexture( Devel::PixelBuffer& pixelBuffer, TextureInfo& textureInfo ) { if( textureInfo.useAtlas != USE_ATLAS ) { DALI_LOG_INFO( gTextureManagerLogFilter, Debug::Concise, " TextureManager::UploadTexture() New Texture for textureId:%d\n", textureInfo.textureId ); - Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight() ); + Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight() ); + PixelData pixelData = Devel::PixelBuffer::Convert( pixelBuffer ); texture.Upload( pixelData ); textureInfo.textureSet = TextureSet::New(); textureInfo.textureSet.SetTexture( 0u, texture ); @@ -472,8 +492,8 @@ void TextureManager::NotifyObservers( TextureInfo& textureInfo, bool success ) { TextureId textureId = textureInfo.textureId; - // If there is an observer: Notify the load is complete, whether successful or not: - // And erase it from the list + // If there is an observer: Notify the load is complete, whether successful or not, + // and erase it from the list unsigned int observerCount = textureInfo.observerList.Count(); TextureInfo* info = &textureInfo; @@ -481,26 +501,29 @@ void TextureManager::NotifyObservers( TextureInfo& textureInfo, bool success ) { TextureUploadObserver* observer = info->observerList[0]; - // During UploadComplete() a Control ResourceReady() signal is emitted - // During that signal the app may add remove /add Textures (e.g. via ImageViews). - // At this point no more observers can be added to the observerList, because textureInfo.loadState = UPLOADED - // However it is possible for observers to be removed, hence we check the observer list count every iteration - - // Also the reference to the textureInfo struct can become invalidated, because new load requests can modify - // the mTextureInfoContainer list (e.g. if more requests are pushed_back it can cause the list to be resized - // invalidating the reference to the TextureInfo ). - observer->UploadComplete( success, info->textureSet, info->useAtlas, info->atlasRect ); + // During UploadComplete() a Control ResourceReady() signal is emitted. + // During that signal the app may add remove /add Textures (e.g. via + // ImageViews). At this point no more observers can be added to the + // observerList, because textureInfo.loadState = UPLOADED. However it is + // possible for observers to be removed, hence we check the observer list + // count every iteration. + + // The reference to the textureInfo struct can also become invalidated, + // because new load requests can modify the mTextureInfoContainer list + // (e.g. if more requests are pushed back it can cause the list to be + // resized invalidating the reference to the TextureInfo ). + observer->UploadComplete( success, info->textureId, info->textureSet, info->useAtlas, info->atlasRect ); observer->DestructionSignal().Disconnect( this, &TextureManager::ObserverDestroyed ); - // regrab the textureInfo from the container as it may have been invalidated, if textures have been removed - // or added during the ResourceReady() signal emission (from UploadComplete() ) - int textureInfoIndex = GetCacheIndexFromId( textureId ); + // Get the textureInfo from the container again as it may have been + // invalidated, + int textureInfoIndex = GetCacheIndexFromId( textureId ); if( textureInfoIndex == INVALID_CACHE_INDEX) { - // texture has been removed - return; + return; // texture has been removed - can stop. } + info = &mTextureInfoContainer[ textureInfoIndex ]; observerCount = info->observerList.Count(); if ( observerCount > 0 ) @@ -513,15 +536,12 @@ void TextureManager::NotifyObservers( TextureInfo& textureInfo, bool success ) info->observerList.Erase( j ); observerCount--; break; - } + } } } - } - } - TextureManager::TextureId TextureManager::GenerateUniqueTextureId() { return mCurrentTextureId++; @@ -643,12 +663,15 @@ void TextureManager::ObserverDestroyed( TextureUploadObserver* observer ) for( unsigned int i = 0; i < count; ++i ) { TextureInfo& textureInfo( mTextureInfoContainer[i] ); - for( TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin(); j != textureInfo.observerList.End(); ++j ) + for( TextureInfo::ObserverListType::Iterator j = textureInfo.observerList.Begin(); j != textureInfo.observerList.End(); ) { if( *j == observer ) { - textureInfo.observerList.Erase( j ); - break; + j = textureInfo.observerList.Erase( j ); + } + else + { + ++j; } } } @@ -656,13 +679,48 @@ void TextureManager::ObserverDestroyed( TextureUploadObserver* observer ) TextureManager::~TextureManager() { - mTextureInfoContainer.clear(); - mAsyncLocalLoadingInfoContainer.clear(); - mAsyncRemoteLoadingInfoContainer.clear(); } +TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(TextureManager& textureManager) +: AsyncLoadingHelper(Toolkit::AsyncImageLoader::New(), textureManager, + AsyncLoadingInfoContainerType()) +{ +} + +void TextureManager::AsyncLoadingHelper::Load(TextureId textureId, + const VisualUrl& url, + ImageDimensions desiredSize, + FittingMode::Type fittingMode, + SamplingMode::Type samplingMode, + bool orientationCorrection) +{ + mLoadingInfoContainer.push_back(AsyncLoadingInfo(textureId)); + auto id = mLoader.Load(url.GetUrl(), desiredSize, fittingMode, samplingMode, orientationCorrection); + mLoadingInfoContainer.back().loadId = id; +} + +TextureManager::AsyncLoadingHelper::AsyncLoadingHelper(AsyncLoadingHelper&& rhs) +: AsyncLoadingHelper(rhs.mLoader, rhs.mTextureManager, std::move(rhs.mLoadingInfoContainer)) +{ +} +TextureManager::AsyncLoadingHelper::AsyncLoadingHelper( + Toolkit::AsyncImageLoader loader, + TextureManager& textureManager, + AsyncLoadingInfoContainerType&& loadingInfoContainer) +: mLoader(loader), + mTextureManager(textureManager), + mLoadingInfoContainer(std::move(loadingInfoContainer)) +{ + DevelAsyncImageLoader::PixelBufferLoadedSignal(mLoader).Connect( + this, &AsyncLoadingHelper::AsyncLoadComplete); +} +void TextureManager::AsyncLoadingHelper::AsyncLoadComplete(uint32_t id, + Devel::PixelBuffer pixelBuffer) +{ + mTextureManager.AsyncLoadComplete(mLoadingInfoContainer, id, pixelBuffer); +} } // namespace Internal