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