+NPatchData* NPatchLoader::GetNPatchData(const VisualUrl& url, const Rect<int>& border, bool& preMultiplyOnLoad)
+{
+ std::size_t hash = CalculateHash(url.GetUrl());
+ std::vector<NPatchInfo>::size_type index = UNINITIALIZED_ID;
+ const std::vector<NPatchInfo>::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<unsigned int>(border.right)) ? info.mData->GetCroppedHeight() - border.right : 0)));
+
+ NPatchUtility::StretchRanges stretchRangesY;
+ stretchRangesY.PushBack(Uint16Pair(border.top, ((info.mData->GetCroppedWidth() >= static_cast<unsigned int>(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;
+}
+