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