Revert of Change SkResourceCache to take a Visitor inside its find(). (patchset #2 id:20001 of https://codereview.chromium.org/567393002/)
Reason for revert:
crashes on android bots, haven't diagnosed yet
Original issue's description:
> Change SkResourceCache to take a Visitor inside its find().
>
> This simplifies the API/contract, in that there are not any exposed
> lock/unlock scopes.
>
>
> patch from issue 572573002
>
> BUG=skia:
>
> Committed: https://skia.googlesource.com/skia/+/dee6a8e67db39fcbde2b3bb09be1d088ebb9db8a
R=mtklein@google.com, danakj@chromium.org
TBR=danakj@chromium.org, mtklein@google.com
NOTREECHECKS=true
NOTRY=true
BUG=skia:
Author: reed@google.com
Review URL: https://codereview.chromium.org/569303002
diff --git a/src/core/SkBitmapCache.cpp b/src/core/SkBitmapCache.cpp
index 84f1036..6d4f4b4 100644
--- a/src/core/SkBitmapCache.cpp
+++ b/src/core/SkBitmapCache.cpp
@@ -59,17 +59,26 @@
virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fBitmap.getSize(); }
-
- static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextBitmap) {
- const BitmapRec& rec = static_cast<const BitmapRec&>(baseRec);
- SkBitmap* result = (SkBitmap*)contextBitmap;
-
- *result = rec.fBitmap;
- result->lockPixels();
- return SkToBool(result->getPixels());
- }
};
+static bool find_and_return(const BitmapKey& key, SkBitmap* result) {
+ const BitmapRec* rec = (BitmapRec*)SkResourceCache::FindAndLock(key);
+ if (rec) {
+ *result = rec->fBitmap;
+ SkResourceCache::Unlock(rec);
+
+ result->lockPixels();
+ if (result->getPixels()) {
+ return true;
+ }
+
+ SkResourceCache::Remove(rec);
+ result->reset();
+ // fall-through to false
+ }
+ return false;
+}
+
bool SkBitmapCache::Find(const SkBitmap& src, SkScalar invScaleX, SkScalar invScaleY,
SkBitmap* result) {
if (0 == invScaleX || 0 == invScaleY) {
@@ -77,7 +86,7 @@
return false;
}
BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_bitmap(src));
- return SkResourceCache::Find(key, BitmapRec::Visitor, result);
+ return find_and_return(key, result);
}
void SkBitmapCache::Add(const SkBitmap& src, SkScalar invScaleX, SkScalar invScaleY,
@@ -93,7 +102,7 @@
bool SkBitmapCache::Find(uint32_t genID, const SkIRect& subset, SkBitmap* result) {
BitmapKey key(genID, SK_Scalar1, SK_Scalar1, subset);
- return SkResourceCache::Find(key, BitmapRec::Visitor, result);
+ return find_and_return(key, result);
}
bool SkBitmapCache::Add(uint32_t genID, const SkIRect& subset, const SkBitmap& result) {
@@ -129,22 +138,16 @@
virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + fMipMap->getSize(); }
-
- static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextMip) {
- const MipMapRec& rec = static_cast<const MipMapRec&>(baseRec);
- const SkMipMap** result = (const SkMipMap**)contextMip;
-
- *result = SkRef(rec.fMipMap);
- // mipmaps don't use the custom allocator yet, so we don't need to check pixels
- return true;
- }
};
+
const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src) {
BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src));
- const SkMipMap* result;
- if (!SkResourceCache::Find(key, MipMapRec::Visitor, &result)) {
- result = NULL;
+ const MipMapRec* rec = (MipMapRec*)SkResourceCache::FindAndLock(key);
+ const SkMipMap* result = NULL;
+ if (rec) {
+ result = SkRef(rec->fMipMap);
+ SkResourceCache::Unlock(rec);
}
return result;
}