reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkBitmapCache.h" |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 9 | #include "SkImage.h" |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 10 | #include "SkResourceCache.h" |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 11 | #include "SkMipMap.h" |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 12 | #include "SkPixelRef.h" |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 13 | #include "SkRect.h" |
| 14 | |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 15 | /** |
| 16 | * Use this for bitmapcache and mipmapcache entries. |
| 17 | */ |
| 18 | uint64_t SkMakeResourceCacheSharedIDForBitmap(uint32_t bitmapGenID) { |
| 19 | uint64_t sharedID = SkSetFourByteTag('b', 'm', 'a', 'p'); |
| 20 | return (sharedID << 32) | bitmapGenID; |
| 21 | } |
| 22 | |
| 23 | void SkNotifyBitmapGenIDIsStale(uint32_t bitmapGenID) { |
| 24 | SkResourceCache::PostPurgeSharedID(SkMakeResourceCacheSharedIDForBitmap(bitmapGenID)); |
| 25 | } |
| 26 | |
| 27 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 28 | |
reed | 14b6aba | 2014-08-29 10:25:26 -0700 | [diff] [blame] | 29 | SkBitmap::Allocator* SkBitmapCache::GetAllocator() { |
| 30 | return SkResourceCache::GetAllocator(); |
| 31 | } |
| 32 | |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 33 | /** |
| 34 | This function finds the bounds of the bitmap *within its pixelRef*. |
| 35 | If the bitmap lacks a pixelRef, it will return an empty rect, since |
| 36 | that doesn't make sense. This may be a useful enough function that |
| 37 | it should be somewhere else (in SkBitmap?). |
| 38 | */ |
| 39 | static SkIRect get_bounds_from_bitmap(const SkBitmap& bm) { |
| 40 | if (!(bm.pixelRef())) { |
| 41 | return SkIRect::MakeEmpty(); |
| 42 | } |
| 43 | SkIPoint origin = bm.pixelRefOrigin(); |
| 44 | return SkIRect::MakeXYWH(origin.fX, origin.fY, bm.width(), bm.height()); |
| 45 | } |
| 46 | |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 47 | /** |
| 48 | * This function finds the bounds of the image. Today this is just the entire bounds, |
| 49 | * but in the future we may support subsets within an image, in which case this should |
| 50 | * return that subset (see get_bounds_from_bitmap). |
| 51 | */ |
| 52 | static SkIRect get_bounds_from_image(const SkImage* image) { |
| 53 | return SkIRect::MakeWH(image->width(), image->height()); |
| 54 | } |
| 55 | |
| 56 | SkBitmapCacheDesc SkBitmapCacheDesc::Make(const SkBitmap& bm, int width, int height) { |
| 57 | SkBitmapCacheDesc desc; |
| 58 | desc.fImageID = bm.getGenerationID(); |
| 59 | desc.fWidth = width; |
| 60 | desc.fHeight = height; |
| 61 | desc.fBounds = get_bounds_from_bitmap(bm); |
| 62 | return desc; |
| 63 | } |
| 64 | |
| 65 | SkBitmapCacheDesc SkBitmapCacheDesc::Make(const SkBitmap& bm) { |
| 66 | return Make(bm, bm.width(), bm.height()); |
| 67 | } |
| 68 | |
| 69 | SkBitmapCacheDesc SkBitmapCacheDesc::Make(const SkImage* image, int width, int height) { |
| 70 | SkBitmapCacheDesc desc; |
| 71 | desc.fImageID = image->uniqueID(); |
| 72 | desc.fWidth = width; |
| 73 | desc.fHeight = height; |
| 74 | desc.fBounds = get_bounds_from_image(image); |
| 75 | return desc; |
| 76 | } |
| 77 | |
| 78 | SkBitmapCacheDesc SkBitmapCacheDesc::Make(const SkImage* image) { |
| 79 | return Make(image, image->width(), image->height()); |
| 80 | } |
| 81 | |
fmalita | 171e5b7 | 2014-10-22 11:20:40 -0700 | [diff] [blame] | 82 | namespace { |
| 83 | static unsigned gBitmapKeyNamespaceLabel; |
| 84 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 85 | struct BitmapKey : public SkResourceCache::Key { |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 86 | public: |
reed | 9913887 | 2015-08-31 15:16:17 -0700 | [diff] [blame] | 87 | BitmapKey(uint32_t genID, int width, int height, const SkIRect& bounds) |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 88 | : fGenID(genID) |
reed | 9913887 | 2015-08-31 15:16:17 -0700 | [diff] [blame] | 89 | , fWidth(width) |
| 90 | , fHeight(height) |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 91 | , fBounds(bounds) |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 92 | { |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 93 | this->init(&gBitmapKeyNamespaceLabel, SkMakeResourceCacheSharedIDForBitmap(fGenID), |
| 94 | sizeof(fGenID) + sizeof(fWidth) + sizeof(fHeight) + sizeof(fBounds)); |
| 95 | } |
| 96 | |
| 97 | BitmapKey(const SkBitmapCacheDesc& desc) |
| 98 | : fGenID(desc.fImageID) |
| 99 | , fWidth(desc.fWidth) |
| 100 | , fHeight(desc.fHeight) |
| 101 | , fBounds(desc.fBounds) |
| 102 | { |
| 103 | this->init(&gBitmapKeyNamespaceLabel, SkMakeResourceCacheSharedIDForBitmap(fGenID), |
reed | 9913887 | 2015-08-31 15:16:17 -0700 | [diff] [blame] | 104 | sizeof(fGenID) + sizeof(fWidth) + sizeof(fHeight) + sizeof(fBounds)); |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 105 | } |
qiankun.miao | 045bb7f | 2014-08-25 06:08:25 -0700 | [diff] [blame] | 106 | |
reed | 995b4bd | 2015-09-14 10:27:57 -0700 | [diff] [blame] | 107 | void dump() const { |
| 108 | SkDebugf("-- add [%d %d] %d [%d %d %d %d]\n", fWidth, fHeight, fGenID, |
| 109 | fBounds.x(), fBounds.y(), fBounds.width(), fBounds.height()); |
| 110 | } |
| 111 | |
reed | 9913887 | 2015-08-31 15:16:17 -0700 | [diff] [blame] | 112 | const uint32_t fGenID; |
| 113 | const int fWidth; |
| 114 | const int fHeight; |
| 115 | const SkIRect fBounds; |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 118 | struct BitmapRec : public SkResourceCache::Rec { |
reed | 9913887 | 2015-08-31 15:16:17 -0700 | [diff] [blame] | 119 | BitmapRec(uint32_t genID, int width, int height, const SkIRect& bounds, |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 120 | const SkBitmap& result) |
reed | 9913887 | 2015-08-31 15:16:17 -0700 | [diff] [blame] | 121 | : fKey(genID, width, height, bounds) |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 122 | , fBitmap(result) |
reed | 995b4bd | 2015-09-14 10:27:57 -0700 | [diff] [blame] | 123 | { |
| 124 | #ifdef TRACE_NEW_BITMAP_CACHE_RECS |
| 125 | fKey.dump(); |
| 126 | #endif |
| 127 | } |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 128 | |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 129 | BitmapRec(const SkBitmapCacheDesc& desc, const SkBitmap& result) |
| 130 | : fKey(desc) |
| 131 | , fBitmap(result) |
| 132 | { |
| 133 | #ifdef TRACE_NEW_BITMAP_CACHE_RECS |
| 134 | fKey.dump(); |
| 135 | #endif |
| 136 | } |
| 137 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 138 | const Key& getKey() const override { return fKey; } |
| 139 | size_t bytesUsed() const override { return sizeof(fKey) + fBitmap.getSize(); } |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 140 | |
reed | 216b643 | 2015-08-19 12:25:40 -0700 | [diff] [blame] | 141 | const char* getCategory() const override { return "bitmap"; } |
| 142 | SkDiscardableMemory* diagnostic_only_getDiscardable() const override { |
| 143 | return fBitmap.pixelRef()->diagnostic_only_getDiscardable(); |
| 144 | } |
| 145 | |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 146 | static bool Finder(const SkResourceCache::Rec& baseRec, void* contextBitmap) { |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 147 | const BitmapRec& rec = static_cast<const BitmapRec&>(baseRec); |
| 148 | SkBitmap* result = (SkBitmap*)contextBitmap; |
reed | 595aa05 | 2014-09-15 10:15:18 -0700 | [diff] [blame] | 149 | |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 150 | *result = rec.fBitmap; |
reed | 595aa05 | 2014-09-15 10:15:18 -0700 | [diff] [blame] | 151 | result->lockPixels(); |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 152 | return SkToBool(result->getPixels()); |
reed | 595aa05 | 2014-09-15 10:15:18 -0700 | [diff] [blame] | 153 | } |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 154 | |
| 155 | private: |
| 156 | BitmapKey fKey; |
| 157 | SkBitmap fBitmap; |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 158 | }; |
fmalita | 171e5b7 | 2014-10-22 11:20:40 -0700 | [diff] [blame] | 159 | } // namespace |
reed | 595aa05 | 2014-09-15 10:15:18 -0700 | [diff] [blame] | 160 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 161 | #define CHECK_LOCAL(localCache, localName, globalName, ...) \ |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 162 | ((localCache) ? localCache->localName(__VA_ARGS__) : SkResourceCache::globalName(__VA_ARGS__)) |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 163 | |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 164 | bool SkBitmapCache::FindWH(const SkBitmapCacheDesc& desc, SkBitmap* result, |
reed | 9913887 | 2015-08-31 15:16:17 -0700 | [diff] [blame] | 165 | SkResourceCache* localCache) { |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 166 | if (0 == desc.fWidth || 0 == desc.fHeight) { |
| 167 | // degenerate |
| 168 | return false; |
| 169 | } |
| 170 | return CHECK_LOCAL(localCache, find, Find, BitmapKey(desc), BitmapRec::Finder, result); |
| 171 | } |
| 172 | |
| 173 | bool SkBitmapCache::AddWH(const SkBitmapCacheDesc& desc, const SkBitmap& result, |
| 174 | SkResourceCache* localCache) { |
| 175 | if (0 == desc.fWidth || 0 == desc.fHeight) { |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 176 | // degenerate, and the key we use for mipmaps |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 177 | return false; |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 178 | } |
reed | 14b6aba | 2014-08-29 10:25:26 -0700 | [diff] [blame] | 179 | SkASSERT(result.isImmutable()); |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 180 | BitmapRec* rec = new BitmapRec(desc, result); |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 181 | CHECK_LOCAL(localCache, add, Add, rec); |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 182 | return true; |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 183 | } |
| 184 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 185 | bool SkBitmapCache::Find(uint32_t genID, const SkIRect& subset, SkBitmap* result, |
| 186 | SkResourceCache* localCache) { |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 187 | BitmapKey key(genID, SK_Scalar1, SK_Scalar1, subset); |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 188 | |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 189 | return CHECK_LOCAL(localCache, find, Find, key, BitmapRec::Finder, result); |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 190 | } |
| 191 | |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 192 | bool SkBitmapCache::Add(SkPixelRef* pr, const SkIRect& subset, const SkBitmap& result, |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 193 | SkResourceCache* localCache) { |
reed | 14b6aba | 2014-08-29 10:25:26 -0700 | [diff] [blame] | 194 | SkASSERT(result.isImmutable()); |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 195 | |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 196 | if (subset.isEmpty() |
| 197 | || subset.top() < 0 |
| 198 | || subset.left() < 0 |
| 199 | || result.width() != subset.width() |
| 200 | || result.height() != subset.height()) { |
| 201 | return false; |
| 202 | } else { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 203 | BitmapRec* rec = new BitmapRec(pr->getGenerationID(), 1, 1, subset, result); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 204 | |
reed | 30ad530 | 2014-09-16 10:39:55 -0700 | [diff] [blame] | 205 | CHECK_LOCAL(localCache, add, Add, rec); |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 206 | pr->notifyAddedToCache(); |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 207 | return true; |
| 208 | } |
| 209 | } |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 210 | |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 211 | bool SkBitmapCache::Find(uint32_t genID, SkBitmap* result, SkResourceCache* localCache) { |
| 212 | BitmapKey key(genID, SK_Scalar1, SK_Scalar1, SkIRect::MakeEmpty()); |
| 213 | |
| 214 | return CHECK_LOCAL(localCache, find, Find, key, BitmapRec::Finder, result); |
| 215 | } |
| 216 | |
| 217 | void SkBitmapCache::Add(uint32_t genID, const SkBitmap& result, SkResourceCache* localCache) { |
| 218 | SkASSERT(result.isImmutable()); |
| 219 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 220 | BitmapRec* rec = new BitmapRec(genID, 1, 1, SkIRect::MakeEmpty(), result); |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 221 | |
| 222 | CHECK_LOCAL(localCache, add, Add, rec); |
| 223 | } |
| 224 | |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 225 | ////////////////////////////////////////////////////////////////////////////////////////// |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 226 | ////////////////////////////////////////////////////////////////////////////////////////// |
| 227 | |
| 228 | namespace { |
| 229 | static unsigned gMipMapKeyNamespaceLabel; |
| 230 | |
| 231 | struct MipMapKey : public SkResourceCache::Key { |
| 232 | public: |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 233 | MipMapKey(uint32_t genID, SkSourceGammaTreatment treatment, const SkIRect& bounds) |
| 234 | : fGenID(genID), fSrcGammaTreatment(static_cast<uint32_t>(treatment)), fBounds(bounds) |
| 235 | { |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 236 | this->init(&gMipMapKeyNamespaceLabel, SkMakeResourceCacheSharedIDForBitmap(genID), |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 237 | sizeof(fGenID) + sizeof(fSrcGammaTreatment) + sizeof(fBounds)); |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | uint32_t fGenID; |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 241 | uint32_t fSrcGammaTreatment; |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 242 | SkIRect fBounds; |
| 243 | }; |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 244 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 245 | struct MipMapRec : public SkResourceCache::Rec { |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 246 | MipMapRec(const SkBitmap& src, SkSourceGammaTreatment treatment, const SkMipMap* result) |
| 247 | : fKey(src.getGenerationID(), treatment, get_bounds_from_bitmap(src)) |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 248 | , fMipMap(result) |
| 249 | { |
| 250 | fMipMap->attachToCacheAndRef(); |
reed | 92561a0 | 2014-10-02 13:47:08 -0700 | [diff] [blame] | 251 | } |
piotaixr | 42b0dfe | 2014-09-03 11:33:13 -0700 | [diff] [blame] | 252 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 253 | virtual ~MipMapRec() { |
| 254 | fMipMap->detachFromCacheAndUnref(); |
| 255 | } |
reed | 37c5a81 | 2014-10-03 13:23:30 -0700 | [diff] [blame] | 256 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 257 | const Key& getKey() const override { return fKey; } |
| 258 | size_t bytesUsed() const override { return sizeof(fKey) + fMipMap->size(); } |
reed | 216b643 | 2015-08-19 12:25:40 -0700 | [diff] [blame] | 259 | const char* getCategory() const override { return "mipmap"; } |
| 260 | SkDiscardableMemory* diagnostic_only_getDiscardable() const override { |
| 261 | return fMipMap->diagnostic_only_getDiscardable(); |
| 262 | } |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 263 | |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 264 | static bool Finder(const SkResourceCache::Rec& baseRec, void* contextMip) { |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 265 | const MipMapRec& rec = static_cast<const MipMapRec&>(baseRec); |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 266 | const SkMipMap* mm = SkRef(rec.fMipMap); |
| 267 | // the call to ref() above triggers a "lock" in the case of discardable memory, |
| 268 | // which means we can now check for null (in case the lock failed). |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 269 | if (nullptr == mm->data()) { |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 270 | mm->unref(); // balance our call to ref() |
| 271 | return false; |
| 272 | } |
| 273 | // the call must call unref() when they are done. |
| 274 | *(const SkMipMap**)contextMip = mm; |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 275 | return true; |
| 276 | } |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 277 | |
| 278 | private: |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 279 | MipMapKey fKey; |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 280 | const SkMipMap* fMipMap; |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 281 | }; |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 282 | } |
reed | 595aa05 | 2014-09-15 10:15:18 -0700 | [diff] [blame] | 283 | |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 284 | const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmapCacheDesc& desc, |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 285 | SkSourceGammaTreatment treatment, |
reed | 98ed7b6 | 2015-09-15 12:38:12 -0700 | [diff] [blame] | 286 | SkResourceCache* localCache) { |
| 287 | // Note: we ignore width/height from desc, just need id and bounds |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 288 | MipMapKey key(desc.fImageID, treatment, desc.fBounds); |
reed | c90e014 | 2014-09-15 11:39:44 -0700 | [diff] [blame] | 289 | const SkMipMap* result; |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 290 | |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 291 | if (!CHECK_LOCAL(localCache, find, Find, key, MipMapRec::Finder, &result)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 292 | result = nullptr; |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 293 | } |
| 294 | return result; |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 295 | } |
| 296 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 297 | static SkResourceCache::DiscardableFactory get_fact(SkResourceCache* localCache) { |
| 298 | return localCache ? localCache->GetDiscardableFactory() |
| 299 | : SkResourceCache::GetDiscardableFactory(); |
| 300 | } |
| 301 | |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 302 | const SkMipMap* SkMipMapCache::AddAndRef(const SkBitmap& src, SkSourceGammaTreatment treatment, |
| 303 | SkResourceCache* localCache) { |
| 304 | SkMipMap* mipmap = SkMipMap::Build(src, treatment, get_fact(localCache)); |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 305 | if (mipmap) { |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 306 | MipMapRec* rec = new MipMapRec(src, treatment, mipmap); |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 307 | CHECK_LOCAL(localCache, add, Add, rec); |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 308 | src.pixelRef()->notifyAddedToCache(); |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 309 | } |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 310 | return mipmap; |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 311 | } |