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 15 matching lines...) Expand all Loading... |
26 } | 26 } |
27 SkIPoint origin = bm.pixelRefOrigin(); | 27 SkIPoint origin = bm.pixelRefOrigin(); |
28 return SkIRect::MakeXYWH(origin.fX, origin.fY, bm.width(), bm.height()); | 28 return SkIRect::MakeXYWH(origin.fX, origin.fY, bm.width(), bm.height()); |
29 } | 29 } |
30 | 30 |
31 namespace { | 31 namespace { |
32 static unsigned gBitmapKeyNamespaceLabel; | 32 static unsigned gBitmapKeyNamespaceLabel; |
33 | 33 |
34 struct BitmapKey : public SkResourceCache::Key { | 34 struct BitmapKey : public SkResourceCache::Key { |
35 public: | 35 public: |
36 BitmapKey(uint32_t genID, SkScalar sx, SkScalar sy, const SkIRect& bounds) | 36 BitmapKey(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& b
ounds) |
37 : fGenID(genID) | 37 : fGenID(genID) |
38 , fScaleX(sx) | 38 , fScaleX(scaleX) |
39 , fScaleY(sy) | 39 , fScaleY(scaleY) |
40 , fBounds(bounds) | 40 , fBounds(bounds) |
41 { | 41 { |
42 this->init(&gBitmapKeyNamespaceLabel, | 42 this->init(&gBitmapKeyNamespaceLabel, |
43 sizeof(fGenID) + sizeof(fScaleX) + sizeof(fScaleY) + sizeof(f
Bounds)); | 43 sizeof(fGenID) + sizeof(fScaleX) + sizeof(fScaleY) + sizeof(f
Bounds)); |
44 } | 44 } |
45 | 45 |
46 uint32_t fGenID; | 46 uint32_t fGenID; |
47 SkScalar fScaleX; | 47 SkScalar fScaleX; |
48 SkScalar fScaleY; | 48 SkScalar fScaleY; |
49 SkIRect fBounds; | 49 SkIRect fBounds; |
50 }; | 50 }; |
51 | 51 |
| 52 ////////////////////////////////////////////////////////////////////////////////
////////// |
| 53 |
52 struct BitmapRec : public SkResourceCache::Rec { | 54 struct BitmapRec : public SkResourceCache::Rec { |
53 BitmapRec(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& b
ounds, | 55 BitmapRec(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& b
ounds, |
54 const SkBitmap& result) | 56 const SkBitmap& result) |
55 : fKey(genID, scaleX, scaleY, bounds) | 57 : fKey(genID, scaleX, scaleY, bounds) |
56 , fBitmap(result) | 58 , fBitmap(result) |
57 {} | 59 {} |
58 | 60 |
| 61 BitmapKey fKey; |
| 62 SkBitmap fBitmap; |
| 63 |
59 const Key& getKey() const SK_OVERRIDE { return fKey; } | 64 const Key& getKey() const SK_OVERRIDE { return fKey; } |
60 size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fBitmap.getSize
(); } | 65 size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fBitmap.getSize
(); } |
61 | 66 |
62 static bool Finder(const SkResourceCache::Rec& baseRec, void* contextBitmap)
{ | 67 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextBitmap
) { |
63 const BitmapRec& rec = static_cast<const BitmapRec&>(baseRec); | 68 const BitmapRec& rec = static_cast<const BitmapRec&>(baseRec); |
64 SkBitmap* result = (SkBitmap*)contextBitmap; | 69 SkBitmap* result = (SkBitmap*)contextBitmap; |
65 | 70 |
66 *result = rec.fBitmap; | 71 *result = rec.fBitmap; |
67 result->lockPixels(); | 72 result->lockPixels(); |
68 return SkToBool(result->getPixels()); | 73 return SkToBool(result->getPixels()); |
69 } | 74 } |
70 | |
71 static SkResourceCache::PurgeVisitorResult PurgeGenID(const SkResourceCache:
:Rec& baseRec, | |
72 void* contextGenID) { | |
73 const BitmapRec& rec = static_cast<const BitmapRec&>(baseRec); | |
74 uintptr_t genID = (uintptr_t)contextGenID; | |
75 if (rec.fKey.fGenID == genID) { | |
76 // SkDebugf("BitmapRec purging genID %d\n", genID); | |
77 return SkResourceCache::kPurgeAndContinue_PurgeVisitorResult; | |
78 } else { | |
79 return SkResourceCache::kRetainAndContinue_PurgeVisitorResult; | |
80 } | |
81 } | |
82 | |
83 private: | |
84 BitmapKey fKey; | |
85 SkBitmap fBitmap; | |
86 }; | 75 }; |
87 } // namespace | 76 } // namespace |
88 | 77 |
89 #define CHECK_LOCAL(localCache, localName, globalName, ...) \ | 78 #define CHECK_LOCAL(localCache, localName, globalName, ...) \ |
90 ((localCache) ? localCache->localName(__VA_ARGS__) : SkResourceCache::global
Name(__VA_ARGS__)) | 79 ((localCache) ? localCache->localName(__VA_ARGS__) : SkResourceCache::global
Name(__VA_ARGS__)) |
91 | 80 |
92 bool SkBitmapCache::Find(const SkBitmap& src, SkScalar invScaleX, SkScalar invSc
aleY, SkBitmap* result, | 81 bool SkBitmapCache::Find(const SkBitmap& src, SkScalar invScaleX, SkScalar invSc
aleY, SkBitmap* result, |
93 SkResourceCache* localCache) { | 82 SkResourceCache* localCache) { |
94 if (0 == invScaleX || 0 == invScaleY) { | 83 if (0 == invScaleX || 0 == invScaleY) { |
95 // degenerate, and the key we use for mipmaps | 84 // degenerate, and the key we use for mipmaps |
96 return false; | 85 return false; |
97 } | 86 } |
98 BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_b
itmap(src)); | 87 BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_b
itmap(src)); |
99 | 88 |
100 return CHECK_LOCAL(localCache, find, Find, key, BitmapRec::Finder, result); | 89 return CHECK_LOCAL(localCache, find, Find, key, BitmapRec::Visitor, result); |
101 } | 90 } |
102 | 91 |
103 void SkBitmapCache::Add(const SkBitmap& src, SkScalar invScaleX, SkScalar invSca
leY, | 92 void SkBitmapCache::Add(const SkBitmap& src, SkScalar invScaleX, SkScalar invSca
leY, |
104 const SkBitmap& result, SkResourceCache* localCache) { | 93 const SkBitmap& result, SkResourceCache* localCache) { |
105 if (0 == invScaleX || 0 == invScaleY) { | 94 if (0 == invScaleX || 0 == invScaleY) { |
106 // degenerate, and the key we use for mipmaps | 95 // degenerate, and the key we use for mipmaps |
107 return; | 96 return; |
108 } | 97 } |
109 SkASSERT(result.isImmutable()); | 98 SkASSERT(result.isImmutable()); |
110 BitmapRec* rec = SkNEW_ARGS(BitmapRec, (src.getGenerationID(), invScaleX, in
vScaleY, | 99 BitmapRec* rec = SkNEW_ARGS(BitmapRec, (src.getGenerationID(), invScaleX, in
vScaleY, |
111 get_bounds_from_bitmap(src), result)
); | 100 get_bounds_from_bitmap(src), result)
); |
112 CHECK_LOCAL(localCache, add, Add, rec); | 101 CHECK_LOCAL(localCache, add, Add, rec); |
113 } | 102 } |
114 | 103 |
115 bool SkBitmapCache::Find(uint32_t genID, const SkIRect& subset, SkBitmap* result
, | 104 bool SkBitmapCache::Find(uint32_t genID, const SkIRect& subset, SkBitmap* result
, |
116 SkResourceCache* localCache) { | 105 SkResourceCache* localCache) { |
117 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, subset); | 106 BitmapKey key(genID, SK_Scalar1, SK_Scalar1, subset); |
118 | 107 |
119 return CHECK_LOCAL(localCache, find, Find, key, BitmapRec::Finder, result); | 108 return CHECK_LOCAL(localCache, find, Find, key, BitmapRec::Visitor, result); |
120 } | 109 } |
121 | 110 |
122 bool SkBitmapCache::Add(uint32_t genID, const SkIRect& subset, const SkBitmap& r
esult, | 111 bool SkBitmapCache::Add(uint32_t genID, const SkIRect& subset, const SkBitmap& r
esult, |
123 SkResourceCache* localCache) { | 112 SkResourceCache* localCache) { |
124 SkASSERT(result.isImmutable()); | 113 SkASSERT(result.isImmutable()); |
125 | 114 |
126 if (subset.isEmpty() | 115 if (subset.isEmpty() |
127 || subset.top() < 0 | 116 || subset.top() < 0 |
128 || subset.left() < 0 | 117 || subset.left() < 0 |
129 || result.width() != subset.width() | 118 || result.width() != subset.width() |
130 || result.height() != subset.height()) { | 119 || result.height() != subset.height()) { |
131 return false; | 120 return false; |
132 } else { | 121 } else { |
133 BitmapRec* rec = SkNEW_ARGS(BitmapRec, (genID, SK_Scalar1, SK_Scalar1, s
ubset, result)); | 122 BitmapRec* rec = SkNEW_ARGS(BitmapRec, (genID, SK_Scalar1, SK_Scalar1, s
ubset, result)); |
134 | 123 |
135 CHECK_LOCAL(localCache, add, Add, rec); | 124 CHECK_LOCAL(localCache, add, Add, rec); |
136 return true; | 125 return true; |
137 } | 126 } |
138 } | 127 } |
139 | |
140 void SkBitmapCache::NotifyGenIDStale(uint32_t genID) { | |
141 SkResourceCache::Purge(&gBitmapKeyNamespaceLabel, BitmapRec::PurgeGenID, | |
142 (void*)((uintptr_t)genID)); | |
143 } | |
144 | |
145 ////////////////////////////////////////////////////////////////////////////////
////////// | 128 ////////////////////////////////////////////////////////////////////////////////
////////// |
146 ////////////////////////////////////////////////////////////////////////////////
////////// | |
147 | |
148 namespace { | |
149 static unsigned gMipMapKeyNamespaceLabel; | |
150 | |
151 struct MipMapKey : public SkResourceCache::Key { | |
152 public: | |
153 MipMapKey(uint32_t genID, const SkIRect& bounds) : fGenID(genID), fBounds(bo
unds) { | |
154 this->init(&gMipMapKeyNamespaceLabel, sizeof(fGenID) + sizeof(fBounds)); | |
155 } | |
156 | |
157 uint32_t fGenID; | |
158 SkIRect fBounds; | |
159 }; | |
160 | 129 |
161 struct MipMapRec : public SkResourceCache::Rec { | 130 struct MipMapRec : public SkResourceCache::Rec { |
162 MipMapRec(const SkBitmap& src, const SkMipMap* result) | 131 MipMapRec(const SkBitmap& src, const SkMipMap* result) |
163 : fKey(src.getGenerationID(), get_bounds_from_bitmap(src)) | 132 : fKey(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)) |
164 , fMipMap(result) | 133 , fMipMap(result) |
165 { | 134 { |
166 fMipMap->attachToCacheAndRef(); | 135 fMipMap->attachToCacheAndRef(); |
167 } | 136 } |
168 | 137 |
169 virtual ~MipMapRec() { | 138 virtual ~MipMapRec() { |
170 fMipMap->detachFromCacheAndUnref(); | 139 fMipMap->detachFromCacheAndUnref(); |
171 } | 140 } |
172 | 141 |
173 const Key& getKey() const SK_OVERRIDE { return fKey; } | 142 const Key& getKey() const SK_OVERRIDE { return fKey; } |
174 size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fMipMap->size()
; } | 143 size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fMipMap->size()
; } |
175 | 144 |
176 static bool Finder(const SkResourceCache::Rec& baseRec, void* contextMip) { | 145 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextMip) { |
177 const MipMapRec& rec = static_cast<const MipMapRec&>(baseRec); | 146 const MipMapRec& rec = static_cast<const MipMapRec&>(baseRec); |
178 const SkMipMap* mm = SkRef(rec.fMipMap); | 147 const SkMipMap* mm = SkRef(rec.fMipMap); |
179 // the call to ref() above triggers a "lock" in the case of discardable
memory, | 148 // the call to ref() above triggers a "lock" in the case of discardable
memory, |
180 // which means we can now check for null (in case the lock failed). | 149 // which means we can now check for null (in case the lock failed). |
181 if (NULL == mm->data()) { | 150 if (NULL == mm->data()) { |
182 mm->unref(); // balance our call to ref() | 151 mm->unref(); // balance our call to ref() |
183 return false; | 152 return false; |
184 } | 153 } |
185 // the call must call unref() when they are done. | 154 // the call must call unref() when they are done. |
186 *(const SkMipMap**)contextMip = mm; | 155 *(const SkMipMap**)contextMip = mm; |
187 return true; | 156 return true; |
188 } | 157 } |
189 | 158 |
190 static SkResourceCache::PurgeVisitorResult PurgeGenID(const SkResourceCache:
:Rec& baseRec, | |
191 void* contextGenID) { | |
192 const MipMapRec& rec = static_cast<const MipMapRec&>(baseRec); | |
193 uintptr_t genID = (uintptr_t)contextGenID; | |
194 if (rec.fKey.fGenID == genID) { | |
195 // SkDebugf("MipMapRec purging genID %d\n", genID); | |
196 return SkResourceCache::kPurgeAndContinue_PurgeVisitorResult; | |
197 } else { | |
198 return SkResourceCache::kRetainAndContinue_PurgeVisitorResult; | |
199 } | |
200 } | |
201 | |
202 private: | 159 private: |
203 MipMapKey fKey; | 160 BitmapKey fKey; |
204 const SkMipMap* fMipMap; | 161 const SkMipMap* fMipMap; |
205 }; | 162 }; |
206 } | |
207 | 163 |
208 const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src, SkResourceCache*
localCache) { | 164 const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src, SkResourceCache*
localCache) { |
209 MipMapKey key(src.getGenerationID(), get_bounds_from_bitmap(src)); | 165 BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src)); |
210 const SkMipMap* result; | 166 const SkMipMap* result; |
211 | 167 |
212 if (!CHECK_LOCAL(localCache, find, Find, key, MipMapRec::Finder, &result)) { | 168 if (!CHECK_LOCAL(localCache, find, Find, key, MipMapRec::Visitor, &result))
{ |
213 result = NULL; | 169 result = NULL; |
214 } | 170 } |
215 return result; | 171 return result; |
216 } | 172 } |
217 | 173 |
218 static SkResourceCache::DiscardableFactory get_fact(SkResourceCache* localCache)
{ | 174 static SkResourceCache::DiscardableFactory get_fact(SkResourceCache* localCache)
{ |
219 return localCache ? localCache->GetDiscardableFactory() | 175 return localCache ? localCache->GetDiscardableFactory() |
220 : SkResourceCache::GetDiscardableFactory(); | 176 : SkResourceCache::GetDiscardableFactory(); |
221 } | 177 } |
222 | 178 |
223 const SkMipMap* SkMipMapCache::AddAndRef(const SkBitmap& src, SkResourceCache* l
ocalCache) { | 179 const SkMipMap* SkMipMapCache::AddAndRef(const SkBitmap& src, SkResourceCache* l
ocalCache) { |
224 SkMipMap* mipmap = SkMipMap::Build(src, get_fact(localCache)); | 180 SkMipMap* mipmap = SkMipMap::Build(src, get_fact(localCache)); |
225 if (mipmap) { | 181 if (mipmap) { |
226 MipMapRec* rec = SkNEW_ARGS(MipMapRec, (src, mipmap)); | 182 MipMapRec* rec = SkNEW_ARGS(MipMapRec, (src, mipmap)); |
227 CHECK_LOCAL(localCache, add, Add, rec); | 183 CHECK_LOCAL(localCache, add, Add, rec); |
228 } | 184 } |
229 return mipmap; | 185 return mipmap; |
230 } | 186 } |
231 | 187 |
232 void SkMipMapCache::NotifyGenIDStale(uint32_t genID) { | |
233 SkResourceCache::Purge(&gMipMapKeyNamespaceLabel, MipMapRec::PurgeGenID, | |
234 (void*)((uintptr_t)genID)); | |
235 } | |
OLD | NEW |