| 81 | } |
| 82 | |
| 83 | PalettedPixels FImageSource::GetCachedPalettedPixels(int conversion, int frame) |
| 84 | { |
| 85 | PalettedPixels ret; |
| 86 | |
| 87 | auto imageID = ImageID; |
| 88 | |
| 89 | // Do we have this image in the cache? |
| 90 | unsigned index = conversion != normal? UINT_MAX : precacheDataPaletted.FindEx([=](PrecacheDataPaletted &entry) { return entry.ImageID == imageID && entry.Frame == frame; }); |
| 91 | if (index < precacheDataPaletted.Size()) |
| 92 | { |
| 93 | auto cache = &precacheDataPaletted[index]; |
| 94 | |
| 95 | if (cache->RefCount > 1) |
| 96 | { |
| 97 | //Printf("returning reference to %s, refcount = %d\n", name.GetChars(), cache->RefCount); |
| 98 | ret.Pixels.Set(cache->Pixels.Data(), cache->Pixels.Size()); |
| 99 | cache->RefCount--; |
| 100 | } |
| 101 | else if (cache->Pixels.Size() > 0) |
| 102 | { |
| 103 | //Printf("returning contents of %s, refcount = %d\n", name.GetChars(), cache->RefCount); |
| 104 | ret = std::move(cache->Pixels); |
| 105 | precacheDataPaletted.Delete(index); |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | //Printf("something bad happened for %s, refcount = %d\n", name.GetChars(), cache->RefCount); |
| 110 | } |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | // The image wasn't cached. Now there's two possibilities: |
| 115 | auto info = precacheInfo.CheckKey(ImageID); |
| 116 | if (!info || info->second <= 1 || conversion != normal) |
| 117 | { |
| 118 | // This is either the only copy needed or some access outside the caching block. In these cases create a new one and directly return it. |
| 119 | //Printf("returning fresh copy of %s\n", name.GetChars()); |
| 120 | return CreatePalettedPixels(conversion, frame); |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | //Printf("creating cached entry for %s, refcount = %d\n", name.GetChars(), info->second); |
| 125 | // This is the first time it gets accessed and needs to be placed in the cache. |
| 126 | PrecacheDataPaletted *pdp = &precacheDataPaletted[precacheDataPaletted.Reserve(1)]; |
| 127 | |
| 128 | pdp->ImageID = imageID; |
| 129 | pdp->RefCount = info->second - 1; |
| 130 | info->second = 0; |
| 131 | pdp->Pixels = CreatePalettedPixels(normal, frame); |
| 132 | ret.Pixels.Set(pdp->Pixels.Data(), pdp->Pixels.Size()); |
| 133 | } |
| 134 | } |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | TArray<uint8_t> FImageSource::GetPalettedPixels(int conversion, int frame) |
| 139 | { |