return SkIRect::MakeXYWH(origin.fX, origin.fY, bm.width(), bm.height());
}
+namespace {
+static unsigned gBitmapKeyNamespaceLabel;
+
struct BitmapKey : public SkResourceCache::Key {
public:
BitmapKey(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& bounds)
, fScaleY(scaleY)
, fBounds(bounds)
{
- this->init(sizeof(fGenID) + sizeof(fScaleX) + sizeof(fScaleY) + sizeof(fBounds));
+ this->init(&gBitmapKeyNamespaceLabel,
+ sizeof(fGenID) + sizeof(fScaleX) + sizeof(fScaleY) + sizeof(fBounds));
}
uint32_t fGenID;
return SkToBool(result->getPixels());
}
};
+} // namespace
#define CHECK_LOCAL(localCache, localName, globalName, ...) \
((localCache) ? localCache->localName(__VA_ARGS__) : SkResourceCache::globalName(__VA_ARGS__))
#include "SkBitmap.h"
#include "SkBitmapProcShader.h"
#include "SkCanvas.h"
-#include "SkDiscardableMemory.h"
#include "SkMatrixUtils.h"
#include "SkPicture.h"
#include "SkReadBuffer.h"
#include "SkResourceCache.h"
-#include "SkThread.h"
#if SK_SUPPORT_GPU
#include "GrContext.h"
#endif
+namespace {
+static unsigned gBitmapSkaderKeyNamespaceLabel;
+
struct BitmapShaderKey : public SkResourceCache::Key {
public:
BitmapShaderKey(uint32_t pictureID,
sizeof(fLocalMatrix);
// This better be packed.
SkASSERT(sizeof(uint32_t) * (&fEndOfStruct - &fPictureID) == keySize);
- this->init(keySize);
+ this->init(&gBitmapSkaderKeyNamespaceLabel, keySize);
}
private:
}
};
-// FIXME: there's considerable boilerplate/duplication here vs. the global resource cache.
-SK_DECLARE_STATIC_MUTEX(gBitmapShaderCacheMutex);
-static SkResourceCache* gBitmapShaderCache = NULL;
-
-#ifndef SK_DEFAULT_TILE_CACHE_LIMIT
- #define SK_DEFAULT_TILE_CACHE_LIMIT (2 * 1024 * 1024)
-#endif
-
-static void cleanup_cache() {
- // We'll clean this up in our own tests, but disable for clients.
- // Chrome seems to have funky multi-process things going on in unit tests that
- // makes this unsafe to delete when the main process atexit()s.
- // SkLazyPtr does the same sort of thing.
-#if SK_DEVELOPER
- SkDELETE(gBitmapShaderCache);
-#endif
-}
-
-/** Must hold gBitmapShaderCacheMutex when calling. */
-static SkResourceCache* cache() {
- // gTileCacheMutex is always held when this is called, so we don't need to be fancy in here.
- gBitmapShaderCacheMutex.assertHeld();
- if (NULL == gBitmapShaderCache) {
-#ifdef SK_USE_DISCARDABLE_SCALEDIMAGECACHE
- gBitmapShaderCache = SkNEW_ARGS(SkResourceCache, (SkDiscardableMemory::Create));
-#else
- gBitmapShaderCache = SkNEW_ARGS(SkResourceCache, (SK_DEFAULT_TILE_CACHE_LIMIT));
-#endif
- atexit(cleanup_cache);
- }
- return gBitmapShaderCache;
-}
-
-static bool cache_find(const BitmapShaderKey& key, SkAutoTUnref<SkShader>* result) {
- SkAutoMutexAcquire am(gBitmapShaderCacheMutex);
- return cache()->find(key, BitmapShaderRec::Visitor, result);
-}
-
-static void cache_add(BitmapShaderRec* rec) {
- SkAutoMutexAcquire am(gBitmapShaderCacheMutex);
- cache()->add(rec);
-}
-
static bool cache_try_alloc_pixels(SkBitmap* bitmap) {
- SkAutoMutexAcquire am(gBitmapShaderCacheMutex);
- SkBitmap::Allocator* allocator = cache()->allocator();
+ SkBitmap::Allocator* allocator = SkResourceCache::GetAllocator();
- if (NULL != allocator) {
- return allocator->allocPixelRef(bitmap, NULL);
- } else {
- return bitmap->tryAllocPixels();
- }
+ return NULL != allocator
+ ? allocator->allocPixelRef(bitmap, NULL)
+ : bitmap->tryAllocPixels();
}
+} // namespace
+
SkPictureShader::SkPictureShader(const SkPicture* picture, TileMode tmx, TileMode tmy,
const SkMatrix* localMatrix, const SkRect* tile)
: INHERITED(localMatrix)
tileScale,
this->getLocalMatrix());
- if (!cache_find(key, &tileShader)) {
+ if (!SkResourceCache::Find(key, BitmapShaderRec::Visitor, &tileShader)) {
SkBitmap bm;
bm.setInfo(SkImageInfo::MakeN32Premul(tileSize));
if (!cache_try_alloc_pixels(&bm)) {
shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height());
tileShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatrix));
- cache_add(SkNEW_ARGS(BitmapShaderRec, (key, tileShader.get(), bm.getSize())));
+ SkResourceCache::Add(SkNEW_ARGS(BitmapShaderRec, (key, tileShader.get(), bm.getSize())));
}
return tileShader.detach();
#include "SkMipMap.h"
#include "SkPixelRef.h"
+#include <stddef.h>
+
// This can be defined by the caller's build system
//#define SK_USE_DISCARDABLE_SCALEDIMAGECACHE
#define SK_DEFAULT_IMAGE_CACHE_LIMIT (2 * 1024 * 1024)
#endif
-void SkResourceCache::Key::init(size_t length) {
+void SkResourceCache::Key::init(void* nameSpace, size_t length) {
SkASSERT(SkAlign4(length) == length);
- // 2 is fCount32 and fHash
- fCount32 = SkToS32(2 + (length >> 2));
- // skip both of our fields whe computing the murmur
- fHash = SkChecksum::Murmur3(this->as32() + 2, (fCount32 - 2) << 2);
+
+ // fCount32 and fHash are not hashed
+ static const int kUnhashedLocal32s = 2;
+ static const int kLocal32s = kUnhashedLocal32s + (sizeof(fNamespace) >> 2);
+
+ SK_COMPILE_ASSERT(sizeof(Key) == (kLocal32s << 2), unaccounted_key_locals);
+ SK_COMPILE_ASSERT(sizeof(Key) == offsetof(Key, fNamespace) + sizeof(fNamespace),
+ namespace_field_must_be_last);
+
+ fCount32 = SkToS32(kLocal32s + (length >> 2));
+ fNamespace = nameSpace;
+ // skip unhashed fields when computing the murmur
+ fHash = SkChecksum::Murmur3(this->as32() + kUnhashedLocal32s,
+ (fCount32 - kUnhashedLocal32s) << 2);
}
#include "SkTDynamicHash.h"
void* writableContents() { return this + 1; }
// must call this after your private data has been written.
+ // nameSpace must be unique per Key subclass.
// length must be a multiple of 4
- void init(size_t length);
+ void init(void* nameSpace, size_t length);
// This is only valid after having called init().
uint32_t hash() const { return fHash; }
}
private:
- int32_t fCount32; // 2 + user contents count32
+ int32_t fCount32; // local + user contents count32
uint32_t fHash;
+ void* fNamespace; // A unique namespace tag. This is hashed.
/* uint32_t fContents32[] */
const uint32_t* as32() const { return (const uint32_t*)this; }