Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmapCache.h" | 8 #include "SkBitmapCache.h" |
| 9 #include "SkResourceCache.h" | 9 #include "SkResourceCache.h" |
| 10 #include "SkMipMap.h" | 10 #include "SkMipMap.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 54 , fBitmap(result) | 54 , fBitmap(result) |
| 55 {} | 55 {} |
| 56 | 56 |
| 57 BitmapKey fKey; | 57 BitmapKey fKey; |
| 58 SkBitmap fBitmap; | 58 SkBitmap fBitmap; |
| 59 | 59 |
| 60 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } | 60 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } |
| 61 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fBitmap .getSize(); } | 61 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fBitmap .getSize(); } |
| 62 }; | 62 }; |
| 63 | 63 |
| 64 struct RRectBlurKey : public SkResourceCache::Key { | |
| 65 public: | |
| 66 RRectBlurKey(SkScalar sigma, const SkRRect& rrect) | |
| 67 : fSigma(sigma) | |
| 68 , fRRect(rrect) { | |
| 69 this->init(sizeof(fSigma) + sizeof(fRRect)); | |
| 70 } | |
| 71 | |
| 72 SkScalar fSigma; | |
| 73 SkRRect fRRect; | |
| 74 }; | |
| 75 | |
| 76 struct RRectBlurRec : public SkResourceCache::Rec { | |
| 77 RRectBlurRec(SkScalar sigma, const SkRRect& rrect, const SkBitmap& result) | |
| 78 : fKey(sigma, rrect) | |
| 79 , fBitmap(result) | |
| 80 {} | |
| 81 | |
| 82 RRectBlurKey fKey; | |
| 83 SkBitmap fBitmap; | |
| 84 | |
| 85 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } | |
| 86 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + sizeof( fBitmap); } | |
|
reed1
2014/09/04 14:15:39
sizeof(fBitmap) does not account for the ram of th
qiankun
2014/09/09 03:03:03
Done.
| |
| 87 }; | |
| 88 | |
| 89 struct RectsBlurKey : public SkResourceCache::Key { | |
| 90 public: | |
| 91 RectsBlurKey(SkScalar sigma, int32_t count, const SkRect rects[]) | |
| 92 : fSigma(sigma) | |
| 93 , fRecCount(count) { | |
| 94 for (int i = 0; i < count; i++) { | |
| 95 fRects[i] = rects[i]; | |
| 96 } | |
| 97 this->init(sizeof(fSigma) + sizeof(fRecCount) + sizeof(SkRect) * fRecCou nt); | |
| 98 } | |
| 99 | |
| 100 SkScalar fSigma; | |
| 101 int32_t fRecCount; | |
| 102 SkRect fRects[2]; | |
| 103 }; | |
| 104 | |
| 105 struct RectsBlurRec : public SkResourceCache::Rec { | |
| 106 RectsBlurRec(SkScalar sigma, unsigned count, const SkRect rects[], const SkB itmap& result) | |
| 107 : fKey(sigma, count, rects) | |
| 108 , fBitmap(result) | |
| 109 {} | |
| 110 | |
| 111 RectsBlurKey fKey; | |
| 112 SkBitmap fBitmap; | |
| 113 | |
| 114 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } | |
| 115 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + sizeof( fBitmap); } | |
| 116 }; | |
| 64 static bool find_and_return(const BitmapKey& key, SkBitmap* result) { | 117 static bool find_and_return(const BitmapKey& key, SkBitmap* result) { |
| 65 const BitmapRec* rec = (BitmapRec*)SkResourceCache::FindAndLock(key); | 118 const BitmapRec* rec = (BitmapRec*)SkResourceCache::FindAndLock(key); |
| 66 if (rec) { | 119 if (rec) { |
| 67 *result = rec->fBitmap; | 120 *result = rec->fBitmap; |
| 68 SkResourceCache::Unlock(rec); | 121 SkResourceCache::Unlock(rec); |
| 69 | 122 |
| 70 result->lockPixels(); | 123 result->lockPixels(); |
| 71 if (result->getPixels()) { | 124 if (result->getPixels()) { |
| 72 return true; | 125 return true; |
| 73 } | 126 } |
| 74 // todo: we should explicitly purge rec from the cache at this point, si nce | 127 // todo: we should explicitly purge rec from the cache at this point, si nce |
| 75 // it is effectively purged already (has no memory behind it) | 128 // it is effectively purged already (has no memory behind it) |
| 76 result->reset(); | 129 result->reset(); |
| 77 // fall-through to false | 130 // fall-through to false |
| 78 } | 131 } |
| 79 return false; | 132 return false; |
| 80 } | 133 } |
| 81 | 134 |
| 135 static bool find_and_return(const RRectBlurKey& key, SkBitmap* result) { | |
| 136 const RRectBlurRec* rec = (RRectBlurRec*)SkResourceCache::FindAndLock(key); | |
| 137 if (rec) { | |
| 138 *result = rec->fBitmap; | |
| 139 SkResourceCache::Unlock(rec); | |
| 140 | |
| 141 result->lockPixels(); | |
| 142 if (result->getPixels()) { | |
| 143 return true; | |
| 144 } | |
| 145 // todo: we should explicitly purge rec from the cache at this point, si nce | |
| 146 // it is effectively purged already (has no memory behind it) | |
| 147 result->reset(); | |
| 148 // fall-through to false | |
| 149 } | |
| 150 return false; | |
| 151 } | |
| 152 | |
| 153 static bool find_and_return(const RectsBlurKey& key, SkBitmap* result) { | |
| 154 const RectsBlurRec* rec = (RectsBlurRec*)SkResourceCache::FindAndLock(key); | |
| 155 if (rec) { | |
| 156 *result = rec->fBitmap; | |
| 157 SkResourceCache::Unlock(rec); | |
| 158 | |
| 159 result->lockPixels(); | |
| 160 if (result->getPixels()) { | |
| 161 return true; | |
| 162 } | |
| 163 // todo: we should explicitly purge rec from the cache at this point, si nce | |
| 164 // it is effectively purged already (has no memory behind it) | |
| 165 result->reset(); | |
| 166 // fall-through to false | |
| 167 } | |
| 168 return false; | |
| 169 } | |
| 170 | |
| 82 bool SkBitmapCache::Find(const SkBitmap& src, SkScalar invScaleX, SkScalar invSc aleY, | 171 bool SkBitmapCache::Find(const SkBitmap& src, SkScalar invScaleX, SkScalar invSc aleY, |
| 83 SkBitmap* result) { | 172 SkBitmap* result) { |
| 84 if (0 == invScaleX || 0 == invScaleY) { | 173 if (0 == invScaleX || 0 == invScaleY) { |
| 85 // degenerate, and the key we use for mipmaps | 174 // degenerate, and the key we use for mipmaps |
| 86 return false; | 175 return false; |
| 87 } | 176 } |
| 88 BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_b itmap(src)); | 177 BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_b itmap(src)); |
| 89 return find_and_return(key, result); | 178 return find_and_return(key, result); |
| 90 } | 179 } |
| 91 | 180 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 114 || result.width() != subset.width() | 203 || result.width() != subset.width() |
| 115 || result.height() != subset.height()) { | 204 || result.height() != subset.height()) { |
| 116 return false; | 205 return false; |
| 117 } else { | 206 } else { |
| 118 SkResourceCache::Add(SkNEW_ARGS(BitmapRec, (genID, SK_Scalar1, SK_Scalar 1, | 207 SkResourceCache::Add(SkNEW_ARGS(BitmapRec, (genID, SK_Scalar1, SK_Scalar 1, |
| 119 subset, result))); | 208 subset, result))); |
| 120 | 209 |
| 121 return true; | 210 return true; |
| 122 } | 211 } |
| 123 } | 212 } |
| 213 | |
| 214 bool SkBitmapCache::Find(SkScalar sigma, const SkRRect& rrect, SkBitmap* result) { | |
| 215 RRectBlurKey key(sigma, rrect); | |
| 216 return find_and_return(key, result); | |
| 217 } | |
| 218 | |
| 219 void SkBitmapCache::Add(SkScalar sigma, const SkRRect& rrect, const SkBitmap& re sult) { | |
| 220 SkResourceCache::Add(SkNEW_ARGS(RRectBlurRec, (sigma, rrect, result))); | |
| 221 } | |
| 222 | |
| 223 bool SkBitmapCache::Find(SkScalar sigma, int32_t count, const SkRect rects[], Sk Bitmap* result) { | |
| 224 RectsBlurKey key(sigma, count, rects); | |
| 225 return find_and_return(key, result); | |
| 226 } | |
| 227 | |
| 228 void SkBitmapCache::Add(SkScalar sigma, int32_t count, const SkRect rects[], con st SkBitmap& result) { | |
| 229 SkResourceCache::Add(SkNEW_ARGS(RectsBlurRec, (sigma, count, rects, result)) ); | |
| 230 } | |
| 231 | |
| 124 //////////////////////////////////////////////////////////////////////////////// ////////// | 232 //////////////////////////////////////////////////////////////////////////////// ////////// |
| 125 | 233 |
| 126 struct MipMapRec : public SkResourceCache::Rec { | 234 struct MipMapRec : public SkResourceCache::Rec { |
| 127 MipMapRec(const SkBitmap& src, const SkMipMap* result) | 235 MipMapRec(const SkBitmap& src, const SkMipMap* result) |
| 128 : fKey(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)) | 236 : fKey(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)) |
| 129 , fMipMap(SkRef(result)) | 237 , fMipMap(SkRef(result)) |
| 130 {} | 238 {} |
| 131 | 239 |
| 132 virtual ~MipMapRec() { | 240 virtual ~MipMapRec() { |
| 133 fMipMap->unref(); | 241 fMipMap->unref(); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 151 } | 259 } |
| 152 return result; | 260 return result; |
| 153 } | 261 } |
| 154 | 262 |
| 155 void SkMipMapCache::Add(const SkBitmap& src, const SkMipMap* result) { | 263 void SkMipMapCache::Add(const SkBitmap& src, const SkMipMap* result) { |
| 156 if (result) { | 264 if (result) { |
| 157 SkResourceCache::Add(SkNEW_ARGS(MipMapRec, (src, result))); | 265 SkResourceCache::Add(SkNEW_ARGS(MipMapRec, (src, result))); |
| 158 } | 266 } |
| 159 } | 267 } |
| 160 | 268 |
| OLD | NEW |