X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fvisuals%2Fnpatch-loader.cpp;h=a7734da6b4901e6dcdece95d80334372225326cf;hp=f0b154a3b82600b5e8c474465fb742b06d5cfb38;hb=HEAD;hpb=369234030acec5230418f620e2c153618d2afe7e diff --git a/dali-toolkit/internal/visuals/npatch-loader.cpp b/dali-toolkit/internal/visuals/npatch-loader.cpp index f0b154a..fb844ce 100644 --- a/dali-toolkit/internal/visuals/npatch-loader.cpp +++ b/dali-toolkit/internal/visuals/npatch-loader.cpp @@ -1,5 +1,5 @@ - /* - * Copyright (c) 2020 Samsung Electronics Co., Ltd. +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,133 +23,104 @@ // EXTERNAL HEADERS #include +#include #include +#include namespace Dali { - namespace Toolkit { - namespace Internal { - namespace { - constexpr auto INVALID_CACHE_INDEX = int32_t{-1}; ///< Invalid Cache index -constexpr auto UNINITIALIZED_ID = int32_t{0}; ///< uninitialised id, use to initialize ids +constexpr auto UNINITIALIZED_ID = int32_t{0}; ///< uninitialised id, use to initialize ids +DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_IMAGE_PERFORMANCE_MARKER, false); } // Anonymous namespace NPatchLoader::NPatchLoader() -: mCurrentNPatchDataId(0) +: mCurrentNPatchDataId(0), + mRemoveProcessorRegistered(false) { } NPatchLoader::~NPatchLoader() { + if(mRemoveProcessorRegistered && Adaptor::IsAvailable()) + { + Adaptor::Get().UnregisterProcessor(*this, true); + mRemoveProcessorRegistered = false; + } } NPatchData::NPatchDataId NPatchLoader::GenerateUniqueNPatchDataId() { + // Skip invalid id generation. + if(DALI_UNLIKELY(mCurrentNPatchDataId == NPatchData::INVALID_NPATCH_DATA_ID)) + { + mCurrentNPatchDataId = 0; + } return mCurrentNPatchDataId++; } -std::size_t NPatchLoader::Load( TextureManager& textureManager, TextureUploadObserver* textureObserver, const std::string& url, const Rect< int >& border, bool& preMultiplyOnLoad, bool synchronousLoading ) +NPatchData::NPatchDataId NPatchLoader::Load(TextureManager& textureManager, TextureUploadObserver* textureObserver, const VisualUrl& url, const Rect& border, bool& preMultiplyOnLoad, bool synchronousLoading) { - std::size_t hash = CalculateHash( url ); - OwnerContainer< NPatchData* >::SizeType index = UNINITIALIZED_ID; - const OwnerContainer< NPatchData* >::SizeType count = mCache.Count(); + NPatchDataPtr data = GetNPatchData(url, border, preMultiplyOnLoad); + + DALI_ASSERT_ALWAYS(data.Get() && "NPatchData creation failed!"); - for( ; index < count; ++index ) + if(data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE) { - if( mCache[ index ]->GetHash() == hash ) + if(!synchronousLoading) { - // hash match, check url as well in case of hash collision - if(mCache[ index ]->GetUrl() == url) + // NotifyObserver already done, so + // data will not iterate observer list. + // We need to call LoadComplete directly. + data->NotifyObserver(textureObserver, true); + } + } + else // if NOT_STARTED or LOADING or LOAD_FAILED, try to reload. + { + if(!synchronousLoading) + { + data->AddObserver(textureObserver); + // If still LOADING and async, don't need to request reload. Fast return. + if(data->GetLoadingState() == NPatchData::LoadingState::LOADING) { - // Use cached data - if( mCache[ index ]->GetBorder() == border ) - { - if( mCache[ index ]->GetLoadingState() == NPatchData::LoadingState::LOADING ) - { - mCache[ index ]->AddObserver( textureObserver ); - } - return mCache[ index ]->GetId(); // valid indices are from 1 onwards - } - else - { - if( mCache[ index ]->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE ) - { - // Same url but border is different - use the existing texture - NPatchData* newData = new NPatchData(); - newData->SetId(GenerateUniqueNPatchDataId()); - newData->SetHash(hash); - newData->SetUrl(url); - newData->SetCroppedWidth(mCache[ index ]->GetCroppedWidth()); - newData->SetCroppedHeight(mCache[ index ]->GetCroppedHeight()); - - newData->SetTextures(mCache[ index ]->GetTextures()); - - NPatchUtility::StretchRanges stretchRangesX; - stretchRangesX.PushBack( Uint16Pair( border.left, ( (newData->GetCroppedWidth() >= static_cast< unsigned int >( border.right )) ? newData->GetCroppedHeight() - border.right : 0 ) ) ); - - NPatchUtility::StretchRanges stretchRangesY; - stretchRangesY.PushBack( Uint16Pair( border.top, ( (newData->GetCroppedWidth() >= static_cast< unsigned int >( border.bottom )) ? newData->GetCroppedHeight() - border.bottom : 0 ) ) ); - - newData->SetStretchPixelsX(stretchRangesX); - newData->SetStretchPixelsY(stretchRangesY); - newData->SetBorder(border); - - newData->SetPreMultiplyOnLoad(mCache[ index ]->IsPreMultiplied()); - - newData->SetLoadingState(NPatchData::LoadingState::LOAD_COMPLETE); - newData->AddObserver( textureObserver ); - - mCache.PushBack( newData ); - - return newData->GetId(); // valid ids start from 1u - } - } + return data->GetId(); } } - } - // If this is new image loading, make new cache data - NPatchData* data; - data = new NPatchData(); - data->SetId(GenerateUniqueNPatchDataId()); - data->SetHash(hash); - data->SetUrl(url); - data->SetBorder(border); - data->SetPreMultiplyOnLoad(preMultiplyOnLoad); - data->AddObserver(textureObserver); - mCache.PushBack(data); + data->SetLoadingState(NPatchData::LoadingState::LOADING); - auto preMultiplyOnLoading = preMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD - : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY; + auto preMultiplyOnLoading = preMultiplyOnLoad ? TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD + : TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY; - Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer( url, Dali::ImageDimensions(), FittingMode::DEFAULT, - SamplingMode::BOX_THEN_LINEAR, synchronousLoading, - data, true, preMultiplyOnLoading ); + Devel::PixelBuffer pixelBuffer = textureManager.LoadPixelBuffer(url, Dali::ImageDimensions(), FittingMode::DEFAULT, SamplingMode::BOX_THEN_LINEAR, synchronousLoading, data.Get(), true, preMultiplyOnLoading); - if( pixelBuffer ) - { - preMultiplyOnLoad = ( preMultiplyOnLoading == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD ) ? true : false; - data->SetLoadedNPatchData( pixelBuffer, preMultiplyOnLoad ); + if(pixelBuffer) + { + preMultiplyOnLoad = (preMultiplyOnLoading == TextureManager::MultiplyOnLoad::MULTIPLY_ON_LOAD) ? true : false; + data->SetLoadedNPatchData(pixelBuffer, preMultiplyOnLoad); + } + else if(synchronousLoading) + { + data->SetLoadingState(NPatchData::LoadingState::LOAD_FAILED); + } } - return data->GetId(); } -int32_t NPatchLoader::GetCacheIndexFromId( const NPatchData::NPatchDataId id ) +int32_t NPatchLoader::GetCacheIndexFromId(const NPatchData::NPatchDataId id) { - const unsigned int size = mCache.Count(); + const unsigned int size = mCache.size(); - for( unsigned int i = 0; i < size; ++i ) + for(unsigned int i = 0; i < size; ++i) { - if( mCache[i]->GetId() == id ) + if(mCache[i].mData->GetId() == id) { return i; } @@ -158,37 +129,184 @@ int32_t NPatchLoader::GetCacheIndexFromId( const NPatchData::NPatchDataId id ) return INVALID_CACHE_INDEX; } -bool NPatchLoader::GetNPatchData( const NPatchData::NPatchDataId id, const NPatchData*& data ) +bool NPatchLoader::GetNPatchData(const NPatchData::NPatchDataId id, NPatchDataPtr& data) { int32_t cacheIndex = GetCacheIndexFromId(id); - if( cacheIndex != INVALID_CACHE_INDEX ) + if(cacheIndex != INVALID_CACHE_INDEX) { - data = mCache[cacheIndex]; + data = mCache[cacheIndex].mData; return true; } data = nullptr; return false; } -void NPatchLoader::Remove( std::size_t id, TextureUploadObserver* textureObserver ) +void NPatchLoader::RequestRemove(NPatchData::NPatchDataId id, TextureUploadObserver* textureObserver) +{ + // Remove observer first + if(textureObserver) + { + int32_t cacheIndex = GetCacheIndexFromId(id); + if(cacheIndex != INVALID_CACHE_INDEX) + { + NPatchInfo& info(mCache[cacheIndex]); + + info.mData->RemoveObserver(textureObserver); + } + } + + mRemoveQueue.push_back({id, nullptr}); + + if(!mRemoveProcessorRegistered && Adaptor::IsAvailable()) + { + mRemoveProcessorRegistered = true; + Adaptor::Get().RegisterProcessor(*this, true); + } +} + +void NPatchLoader::Remove(NPatchData::NPatchDataId id, TextureUploadObserver* textureObserver) { int32_t cacheIndex = GetCacheIndexFromId(id); - if( cacheIndex == INVALID_CACHE_INDEX ) + if(cacheIndex == INVALID_CACHE_INDEX) { return; } - NPatchData* data; - data = mCache[cacheIndex]; + NPatchInfo& info(mCache[cacheIndex]); - data->RemoveObserver(textureObserver); + info.mData->RemoveObserver(textureObserver); - if(data->GetObserverCount() == 0) + if(--info.mReferenceCount <= 0) { - mCache.Erase( mCache.Begin() + cacheIndex ); + mCache.erase(mCache.begin() + cacheIndex); } } +void NPatchLoader::Process(bool postProcessor) +{ + DALI_TRACE_BEGIN_WITH_MESSAGE_GENERATOR(gTraceFilter, "DALI_NPATCH_LOADER_PROCESS_REMOVE_QUEUE", [&](std::ostringstream& oss) { + oss << "[" << mRemoveQueue.size() << "]"; + }); + + for(auto& iter : mRemoveQueue) + { + Remove(iter.first, iter.second); + } + + mRemoveQueue.clear(); + + if(Adaptor::IsAvailable()) + { + Adaptor::Get().UnregisterProcessor(*this, true); + mRemoveProcessorRegistered = false; + } + + DALI_TRACE_END(gTraceFilter, "DALI_NPATCH_LOADER_PROCESS_REMOVE_QUEUE"); +} + +NPatchDataPtr NPatchLoader::GetNPatchData(const VisualUrl& url, const Rect& border, bool& preMultiplyOnLoad) +{ + std::size_t hash = CalculateHash(url.GetUrl()); + std::vector::size_type index = UNINITIALIZED_ID; + const std::vector::size_type count = mCache.size(); + + NPatchInfo* infoPtr = nullptr; + + for(; index < count; ++index) + { + if(mCache[index].mData->GetHash() == hash) + { + // hash match, check url as well in case of hash collision + if(mCache[index].mData->GetUrl().GetUrl() == url.GetUrl()) + { + // Use cached data. Need to fast-out return. + if(mCache[index].mData->GetBorder() == border) + { + mCache[index].mReferenceCount++; + return mCache[index].mData; + } + else + { + if(mCache[index].mData->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE) + { + // If we only found LOAD_FAILED case, replace current data. We can reuse texture + if(infoPtr == nullptr || infoPtr->mData->GetLoadingState() != NPatchData::LoadingState::LOAD_COMPLETE) + { + infoPtr = &mCache[index]; + } + } + // Still loading pixel buffer. We cannot reuse cached texture yet. Skip checking + else if(mCache[index].mData->GetLoadingState() == NPatchData::LoadingState::LOADING) + { + continue; + } + // if LOAD_FAILED, reuse this cached NPatchData, and try to load again. + else + { + if(infoPtr == nullptr) + { + infoPtr = &mCache[index]; + } + } + } + } + } + } + + // If this is new image loading, make new cache data + if(infoPtr == nullptr) + { + NPatchInfo info(new NPatchData()); + info.mData->SetId(GenerateUniqueNPatchDataId()); + info.mData->SetHash(hash); + info.mData->SetUrl(url); + info.mData->SetBorder(border); + info.mData->SetPreMultiplyOnLoad(preMultiplyOnLoad); + + mCache.emplace_back(std::move(info)); + infoPtr = &mCache.back(); + } + // Else if LOAD_COMPLETE, Same url but border is different - use the existing texture + else if(infoPtr->mData->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE) + { + NPatchInfo info(new NPatchData()); + + info.mData->SetId(GenerateUniqueNPatchDataId()); + info.mData->SetHash(hash); + info.mData->SetUrl(url); + info.mData->SetCroppedWidth(infoPtr->mData->GetCroppedWidth()); + info.mData->SetCroppedHeight(infoPtr->mData->GetCroppedHeight()); + + info.mData->SetTextures(infoPtr->mData->GetTextures()); + + NPatchUtility::StretchRanges stretchRangesX; + stretchRangesX.PushBack(Uint16Pair(border.left, ((info.mData->GetCroppedWidth() >= static_cast(border.right)) ? info.mData->GetCroppedHeight() - border.right : 0))); + + NPatchUtility::StretchRanges stretchRangesY; + stretchRangesY.PushBack(Uint16Pair(border.top, ((info.mData->GetCroppedWidth() >= static_cast(border.bottom)) ? info.mData->GetCroppedHeight() - border.bottom : 0))); + + info.mData->SetStretchPixelsX(stretchRangesX); + info.mData->SetStretchPixelsY(stretchRangesY); + info.mData->SetBorder(border); + + info.mData->SetPreMultiplyOnLoad(infoPtr->mData->IsPreMultiplied()); + + info.mData->SetLoadingState(NPatchData::LoadingState::LOAD_COMPLETE); + + mCache.emplace_back(std::move(info)); + infoPtr = &mCache.back(); + } + // Else, LOAD_FAILED. just increase reference so we can reuse it. + else + { + infoPtr->mReferenceCount++; + } + + DALI_ASSERT_ALWAYS(infoPtr && "NPatchInfo creation failed!"); + + return infoPtr->mData; +} + } // namespace Internal } // namespace Toolkit