- Data* data = new Data();
- data->hash = hash;
- data->url = url;
- data->textureSet = TextureSet::New();
- TextureSetImage( data->textureSet, 0u, croppedImage );
- data->croppedWidth = croppedImage.GetWidth();
- data->croppedHeight = croppedImage.GetHeight();
- data->stretchPixelsX = ninePatch.GetStretchPixelsX();
- data->stretchPixelsY = ninePatch.GetStretchPixelsY();
- data->border = Rect< int >( 0, 0, 0, 0 );
- mCache.PushBack( data );
-
- return mCache.Count(); // valid ids start from 1u
+ // 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];
+ }
+ }
+ }