| 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 const SkBitmap& result) | 52 const SkBitmap& result) |
| 53 : fKey(genID, scaleX, scaleY, bounds) | 53 : fKey(genID, scaleX, scaleY, bounds) |
| 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 |
| 63 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextBitmap
) { |
| 64 const BitmapRec& rec = static_cast<const BitmapRec&>(baseRec); |
| 65 SkBitmap* result = (SkBitmap*)contextBitmap; |
| 66 |
| 67 *result = rec.fBitmap; |
| 68 result->lockPixels(); |
| 69 return SkToBool(result->getPixels()); |
| 70 } |
| 62 }; | 71 }; |
| 63 | 72 |
| 64 static bool find_and_return(const BitmapKey& key, SkBitmap* result) { | |
| 65 const BitmapRec* rec = (BitmapRec*)SkResourceCache::FindAndLock(key); | |
| 66 if (rec) { | |
| 67 *result = rec->fBitmap; | |
| 68 SkResourceCache::Unlock(rec); | |
| 69 | |
| 70 result->lockPixels(); | |
| 71 if (result->getPixels()) { | |
| 72 return true; | |
| 73 } | |
| 74 | |
| 75 SkResourceCache::Remove(rec); | |
| 76 result->reset(); | |
| 77 // fall-through to false | |
| 78 } | |
| 79 return false; | |
| 80 } | |
| 81 | |
| 82 bool SkBitmapCache::Find(const SkBitmap& src, SkScalar invScaleX, SkScalar invSc
aleY, | 73 bool SkBitmapCache::Find(const SkBitmap& src, SkScalar invScaleX, SkScalar invSc
aleY, |
| 83 SkBitmap* result) { | 74 SkBitmap* result) { |
| 84 if (0 == invScaleX || 0 == invScaleY) { | 75 if (0 == invScaleX || 0 == invScaleY) { |
| 85 // degenerate, and the key we use for mipmaps | 76 // degenerate, and the key we use for mipmaps |
| 86 return false; | 77 return false; |
| 87 } | 78 } |
| 88 BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_b
itmap(src)); | 79 BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_b
itmap(src)); |
| 89 return find_and_return(key, result); | 80 return SkResourceCache::Find(key, BitmapRec::Visitor, result); |
| 90 } | 81 } |
| 91 | 82 |
| 92 void SkBitmapCache::Add(const SkBitmap& src, SkScalar invScaleX, SkScalar invSca
leY, | 83 void SkBitmapCache::Add(const SkBitmap& src, SkScalar invScaleX, SkScalar invSca
leY, |
| 93 const SkBitmap& result) { | 84 const SkBitmap& result) { |
| 94 if (0 == invScaleX || 0 == invScaleY) { | 85 if (0 == invScaleX || 0 == invScaleY) { |
| 95 // degenerate, and the key we use for mipmaps | 86 // degenerate, and the key we use for mipmaps |
| 96 return; | 87 return; |
| 97 } | 88 } |
| 98 SkASSERT(result.isImmutable()); | 89 SkASSERT(result.isImmutable()); |
| 99 SkResourceCache::Add(SkNEW_ARGS(BitmapRec, (src.getGenerationID(), invScaleX
, invScaleY, | 90 SkResourceCache::Add(SkNEW_ARGS(BitmapRec, (src.getGenerationID(), invScaleX
, invScaleY, |
| 100 get_bounds_from_bitmap(src), res
ult))); | 91 get_bounds_from_bitmap(src), res
ult))); |
| 101 } | 92 } |
| 102 | 93 |
| 103 bool SkBitmapCache::Find(uint32_t genID, const SkIRect& subset, SkBitmap* result
) { | 94 bool SkBitmapCache::Find(uint32_t genID, const SkIRect& subset, SkBitmap* result
) { |
| 104 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, subset); | 95 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, subset); |
| 105 return find_and_return(key, result); | 96 return SkResourceCache::Find(key, BitmapRec::Visitor, result); |
| 106 } | 97 } |
| 107 | 98 |
| 108 bool SkBitmapCache::Add(uint32_t genID, const SkIRect& subset, const SkBitmap& r
esult) { | 99 bool SkBitmapCache::Add(uint32_t genID, const SkIRect& subset, const SkBitmap& r
esult) { |
| 109 SkASSERT(result.isImmutable()); | 100 SkASSERT(result.isImmutable()); |
| 110 | 101 |
| 111 if (subset.isEmpty() | 102 if (subset.isEmpty() |
| 112 || subset.top() < 0 | 103 || subset.top() < 0 |
| 113 || subset.left() < 0 | 104 || subset.left() < 0 |
| 114 || result.width() != subset.width() | 105 || result.width() != subset.width() |
| 115 || result.height() != subset.height()) { | 106 || result.height() != subset.height()) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 131 | 122 |
| 132 virtual ~MipMapRec() { | 123 virtual ~MipMapRec() { |
| 133 fMipMap->unref(); | 124 fMipMap->unref(); |
| 134 } | 125 } |
| 135 | 126 |
| 136 BitmapKey fKey; | 127 BitmapKey fKey; |
| 137 const SkMipMap* fMipMap; | 128 const SkMipMap* fMipMap; |
| 138 | 129 |
| 139 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } | 130 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } |
| 140 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fMipMap
->getSize(); } | 131 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fMipMap
->getSize(); } |
| 132 |
| 133 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextMip) { |
| 134 const MipMapRec& rec = static_cast<const MipMapRec&>(baseRec); |
| 135 const SkMipMap** result = (const SkMipMap**)contextMip; |
| 136 |
| 137 *result = SkRef(rec.fMipMap); |
| 138 // mipmaps don't use the custom allocator yet, so we don't need to check
pixels |
| 139 return true; |
| 140 } |
| 141 }; | 141 }; |
| 142 | 142 |
| 143 | |
| 144 const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src) { | 143 const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src) { |
| 145 BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)); | 144 BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)); |
| 146 const MipMapRec* rec = (MipMapRec*)SkResourceCache::FindAndLock(key); | 145 const SkMipMap* result; |
| 147 const SkMipMap* result = NULL; | 146 if (!SkResourceCache::Find(key, MipMapRec::Visitor, &result)) { |
| 148 if (rec) { | 147 result = NULL; |
| 149 result = SkRef(rec->fMipMap); | |
| 150 SkResourceCache::Unlock(rec); | |
| 151 } | 148 } |
| 152 return result; | 149 return result; |
| 153 } | 150 } |
| 154 | 151 |
| 155 void SkMipMapCache::Add(const SkBitmap& src, const SkMipMap* result) { | 152 void SkMipMapCache::Add(const SkBitmap& src, const SkMipMap* result) { |
| 156 if (result) { | 153 if (result) { |
| 157 SkResourceCache::Add(SkNEW_ARGS(MipMapRec, (src, result))); | 154 SkResourceCache::Add(SkNEW_ARGS(MipMapRec, (src, result))); |
| 158 } | 155 } |
| 159 } | 156 } |
| 160 | 157 |
| OLD | NEW |