diff options
-rw-r--r-- | include/core/SkBitmapDevice.h | 2 | ||||
-rw-r--r-- | include/core/SkDevice.h | 2 | ||||
-rw-r--r-- | include/core/SkImageFilter.h | 24 | ||||
-rw-r--r-- | include/core/SkPicture.h | 3 | ||||
-rw-r--r-- | include/core/SkReadBuffer.h | 1 | ||||
-rw-r--r-- | include/gpu/SkGpuDevice.h | 2 | ||||
-rw-r--r-- | src/core/SkBitmapDevice.cpp | 6 | ||||
-rw-r--r-- | src/core/SkCanvas.cpp | 18 | ||||
-rw-r--r-- | src/core/SkImageFilter.cpp | 168 | ||||
-rw-r--r-- | src/gpu/SkGpuDevice.cpp | 18 | ||||
-rw-r--r-- | tests/ImageFilterTest.cpp | 3 |
11 files changed, 216 insertions, 31 deletions
diff --git a/include/core/SkBitmapDevice.h b/include/core/SkBitmapDevice.h index e1765e56ab..5dde9e068b 100644 --- a/include/core/SkBitmapDevice.h +++ b/include/core/SkBitmapDevice.h @@ -156,6 +156,8 @@ private: virtual SkSurface* newSurface(const SkImageInfo&) SK_OVERRIDE; virtual const void* peekPixels(SkImageInfo*, size_t* rowBytes) SK_OVERRIDE; + virtual SkImageFilter::UniqueIDCache* getImageFilterCache() SK_OVERRIDE; + SkBitmap fBitmap; typedef SkBaseDevice INHERITED; diff --git a/include/core/SkDevice.h b/include/core/SkDevice.h index 4a9edee0ec..504138df1e 100644 --- a/include/core/SkDevice.h +++ b/include/core/SkDevice.h @@ -378,6 +378,8 @@ private: */ virtual void flush() {} + virtual SkImageFilter::UniqueIDCache* getImageFilterCache() { return NULL; } + SkIPoint fOrigin; SkMetaData* fMetaData; diff --git a/include/core/SkImageFilter.h b/include/core/SkImageFilter.h index e7ba5e6ad9..d2a4d3e3d9 100644 --- a/include/core/SkImageFilter.h +++ b/include/core/SkImageFilter.h @@ -61,18 +61,30 @@ public: virtual void remove(const SkImageFilter* key) = 0; }; + // This cache maps from (filter's unique ID + CTM + clipBounds + src bitmap generation ID) to + // (result, offset). + class UniqueIDCache : public SkRefCnt { + public: + struct Key; + virtual ~UniqueIDCache() {} + static UniqueIDCache* Create(size_t maxBytes); + static UniqueIDCache* Get(); + virtual bool get(const Key& key, SkBitmap* result, SkIPoint* offset) const = 0; + virtual void set(const Key& key, const SkBitmap& result, const SkIPoint& offset) = 0; + }; + class Context { public: - Context(const SkMatrix& ctm, const SkIRect& clipBounds, Cache* cache) : + Context(const SkMatrix& ctm, const SkIRect& clipBounds, UniqueIDCache* cache) : fCTM(ctm), fClipBounds(clipBounds), fCache(cache) { } const SkMatrix& ctm() const { return fCTM; } const SkIRect& clipBounds() const { return fClipBounds; } - Cache* cache() const { return fCache; } + UniqueIDCache* cache() const { return fCache; } private: SkMatrix fCTM; SkIRect fClipBounds; - Cache* fCache; + UniqueIDCache* fCache; }; class Proxy { @@ -210,6 +222,7 @@ protected: CropRect cropRect() const { return fCropRect; } int inputCount() const { return fInputs.count(); } SkImageFilter** inputs() const { return fInputs.get(); } + uint32_t uniqueID() const { return fUniqueID; } // If the caller wants a copy of the inputs, call this and it will transfer ownership // of the unflattened input filters to the caller. This is just a short-cut for copying @@ -221,6 +234,7 @@ protected: CropRect fCropRect; // most filters accept at most 2 input-filters SkAutoSTArray<2, SkImageFilter*> fInputs; + uint32_t fUniqueID; void allocInputs(int count); }; @@ -308,10 +322,14 @@ protected: const SkIRect& bounds) const; private: + bool usesSrcInput() const { return fUsesSrcInput; } + typedef SkFlattenable INHERITED; int fInputCount; SkImageFilter** fInputs; + bool fUsesSrcInput; CropRect fCropRect; + uint32_t fUniqueID; // Globally unique }; #endif diff --git a/include/core/SkPicture.h b/include/core/SkPicture.h index 21ebef32cd..c733e532c2 100644 --- a/include/core/SkPicture.h +++ b/include/core/SkPicture.h @@ -230,13 +230,14 @@ private: // V28: No longer call bitmap::flatten inside SkWriteBuffer::writeBitmap. // V29: Removed SaveFlags parameter from save(). // V30: Remove redundant SkMatrix from SkLocalMatrixShader. + // V31: Add a serialized UniqueID to SkImageFilter. // Note: If the picture version needs to be increased then please follow the // steps to generate new SKPs in (only accessible to Googlers): http://goo.gl/qATVcw // Only SKPs within the min/current picture version range (inclusive) can be read. static const uint32_t MIN_PICTURE_VERSION = 19; - static const uint32_t CURRENT_PICTURE_VERSION = 30; + static const uint32_t CURRENT_PICTURE_VERSION = 31; mutable uint32_t fUniqueID; diff --git a/include/core/SkReadBuffer.h b/include/core/SkReadBuffer.h index faf7eb81a5..2beb7ac738 100644 --- a/include/core/SkReadBuffer.h +++ b/include/core/SkReadBuffer.h @@ -46,6 +46,7 @@ public: kNoUnitMappers_Version = 27, kNoMoreBitmapFlatten_Version = 28, kSimplifyLocalMatrix_Version = 30, + kImageFilterUniqueID_Version = 31, }; /** diff --git a/include/gpu/SkGpuDevice.h b/include/gpu/SkGpuDevice.h index 9a5a92e2df..b43213a8d7 100644 --- a/include/gpu/SkGpuDevice.h +++ b/include/gpu/SkGpuDevice.h @@ -169,6 +169,8 @@ private: virtual SkSurface* newSurface(const SkImageInfo&) SK_OVERRIDE; + virtual SkImageFilter::UniqueIDCache* getImageFilterCache() SK_OVERRIDE; + // sets the render target, clip, and matrix on GrContext. Use forceIdenity to override // SkDraw's matrix and draw in device coords. void prepareDraw(const SkDraw&, bool forceIdentity); diff --git a/src/core/SkBitmapDevice.cpp b/src/core/SkBitmapDevice.cpp index 09b3b605f8..8c92f71dce 100644 --- a/src/core/SkBitmapDevice.cpp +++ b/src/core/SkBitmapDevice.cpp @@ -367,6 +367,12 @@ const void* SkBitmapDevice::peekPixels(SkImageInfo* info, size_t* rowBytes) { return NULL; } +SkImageFilter::UniqueIDCache* SkBitmapDevice::getImageFilterCache() { + SkImageFilter::UniqueIDCache* cache = SkImageFilter::UniqueIDCache::Get(); + cache->ref(); + return cache; +} + /////////////////////////////////////////////////////////////////////////////// bool SkBitmapDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) { diff --git a/src/core/SkCanvas.cpp b/src/core/SkCanvas.cpp index 504c9908ab..a561316b7f 100644 --- a/src/core/SkCanvas.cpp +++ b/src/core/SkCanvas.cpp @@ -1134,13 +1134,8 @@ void SkCanvas::internalDrawDevice(SkBaseDevice* srcDev, int x, int y, SkMatrix matrix = *iter.fMatrix; matrix.postTranslate(SkIntToScalar(-pos.x()), SkIntToScalar(-pos.y())); SkIRect clipBounds = SkIRect::MakeWH(srcDev->width(), srcDev->height()); - SkImageFilter::Cache* cache = SkImageFilter::GetExternalCache(); - SkAutoUnref aur(NULL); - if (!cache) { - cache = SkImageFilter::Cache::Create(); - aur.reset(cache); - } - SkImageFilter::Context ctx(matrix, clipBounds, cache); + SkAutoTUnref<SkImageFilter::UniqueIDCache> cache(dstDev->getImageFilterCache()); + SkImageFilter::Context ctx(matrix, clipBounds, cache.get()); if (filter->filterImage(&proxy, src, ctx, &dst, &offset)) { SkPaint tmpUnfiltered(*paint); tmpUnfiltered.setImageFilter(NULL); @@ -1179,13 +1174,8 @@ void SkCanvas::drawSprite(const SkBitmap& bitmap, int x, int y, SkMatrix matrix = *iter.fMatrix; matrix.postTranslate(SkIntToScalar(-pos.x()), SkIntToScalar(-pos.y())); SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height()); - SkImageFilter::Cache* cache = SkImageFilter::GetExternalCache(); - SkAutoUnref aur(NULL); - if (!cache) { - cache = SkImageFilter::Cache::Create(); - aur.reset(cache); - } - SkImageFilter::Context ctx(matrix, clipBounds, cache); + SkAutoTUnref<SkImageFilter::UniqueIDCache> cache(iter.fDevice->getImageFilterCache()); + SkImageFilter::Context ctx(matrix, clipBounds, cache.get()); if (filter->filterImage(&proxy, bitmap, ctx, &dst, &offset)) { SkPaint tmpUnfiltered(*paint); tmpUnfiltered.setImageFilter(NULL); diff --git a/src/core/SkImageFilter.cpp b/src/core/SkImageFilter.cpp index 11a1420dfa..5fa6855a65 100644 --- a/src/core/SkImageFilter.cpp +++ b/src/core/SkImageFilter.cpp @@ -10,10 +10,12 @@ #include "SkBitmap.h" #include "SkChecksum.h" #include "SkDevice.h" +#include "SkLazyPtr.h" #include "SkReadBuffer.h" #include "SkWriteBuffer.h" #include "SkRect.h" #include "SkTDynamicHash.h" +#include "SkTInternalLList.h" #include "SkValidationUtils.h" #if SK_SUPPORT_GPU #include "GrContext.h" @@ -21,6 +23,39 @@ #include "SkGr.h" #endif +enum { kDefaultCacheSize = 128 * 1024 * 1024 }; + +static int32_t next_image_filter_unique_id() { + static int32_t gImageFilterUniqueID; + + // Never return 0. + int32_t id; + do { + id = sk_atomic_inc(&gImageFilterUniqueID) + 1; + } while (0 == id); + return id; +} + +struct SkImageFilter::UniqueIDCache::Key { + Key(const uint32_t uniqueID, const SkMatrix& matrix, const SkIRect& clipBounds, uint32_t srcGenID) + : fUniqueID(uniqueID), fMatrix(matrix), fClipBounds(clipBounds), fSrcGenID(srcGenID) { + // Assert that Key is tightly-packed, since it is hashed. + SK_COMPILE_ASSERT(sizeof(Key) == sizeof(uint32_t) + sizeof(SkMatrix) + sizeof(SkIRect) + + sizeof(uint32_t), image_filter_key_tight_packing); + fMatrix.getType(); // force initialization of type, so hashes match + } + uint32_t fUniqueID; + SkMatrix fMatrix; + SkIRect fClipBounds; + uint32_t fSrcGenID; + bool operator==(const Key& other) const { + return fUniqueID == other.fUniqueID + && fMatrix == other.fMatrix + && fClipBounds == other.fClipBounds + && fSrcGenID == other.fSrcGenID; + } +}; + SkImageFilter::Common::~Common() { for (int i = 0; i < fInputs.count(); ++i) { SkSafeUnref(fInputs[i]); @@ -65,6 +100,11 @@ bool SkImageFilter::Common::unflatten(SkReadBuffer& buffer, int expectedCount) { uint32_t flags = buffer.readUInt(); fCropRect = CropRect(rect, flags); + if (buffer.isVersionLT(SkReadBuffer::kImageFilterUniqueID_Version)) { + fUniqueID = next_image_filter_unique_id(); + } else { + fUniqueID = buffer.readUInt(); + } return buffer.isValid(); } @@ -75,8 +115,13 @@ SkImageFilter::Cache* gExternalCache; SkImageFilter::SkImageFilter(int inputCount, SkImageFilter** inputs, const CropRect* cropRect) : fInputCount(inputCount), fInputs(new SkImageFilter*[inputCount]), - fCropRect(cropRect ? *cropRect : CropRect(SkRect(), 0x0)) { + fUsesSrcInput(false), + fCropRect(cropRect ? *cropRect : CropRect(SkRect(), 0x0)), + fUniqueID(next_image_filter_unique_id()) { for (int i = 0; i < inputCount; ++i) { + if (NULL == inputs[i] || inputs[i]->usesSrcInput()) { + fUsesSrcInput = true; + } fInputs[i] = inputs[i]; SkSafeRef(fInputs[i]); } @@ -89,13 +134,20 @@ SkImageFilter::~SkImageFilter() { delete[] fInputs; } -SkImageFilter::SkImageFilter(int inputCount, SkReadBuffer& buffer) { +SkImageFilter::SkImageFilter(int inputCount, SkReadBuffer& buffer) + : fUsesSrcInput(false) { Common common; if (common.unflatten(buffer, inputCount)) { fCropRect = common.cropRect(); fInputCount = common.inputCount(); fInputs = SkNEW_ARRAY(SkImageFilter*, fInputCount); common.detachInputs(fInputs); + for (int i = 0; i < fInputCount; ++i) { + if (NULL == fInputs[i] || fInputs[i]->usesSrcInput()) { + fUsesSrcInput = true; + } + } + fUniqueID = buffer.isCrossProcess() ? next_image_filter_unique_id() : common.uniqueID(); } else { fInputCount = 0; fInputs = NULL; @@ -113,17 +165,25 @@ void SkImageFilter::flatten(SkWriteBuffer& buffer) const { } buffer.writeRect(fCropRect.rect()); buffer.writeUInt(fCropRect.flags()); + buffer.writeUInt(fUniqueID); } bool SkImageFilter::filterImage(Proxy* proxy, const SkBitmap& src, const Context& context, SkBitmap* result, SkIPoint* offset) const { - Cache* cache = context.cache(); SkASSERT(result); SkASSERT(offset); - SkASSERT(cache); - if (cache->get(this, result, offset)) { - return true; + uint32_t srcGenID = fUsesSrcInput ? src.getGenerationID() : 0; + Cache* externalCache = GetExternalCache(); + UniqueIDCache::Key key(fUniqueID, context.ctm(), context.clipBounds(), srcGenID); + if (NULL != externalCache) { + if (externalCache->get(this, result, offset)) { + return true; + } + } else if (context.cache()) { + if (context.cache()->get(key, result, offset)) { + return true; + } } /* * Give the proxy first shot at the filter. If it returns false, ask @@ -131,7 +191,11 @@ bool SkImageFilter::filterImage(Proxy* proxy, const SkBitmap& src, */ if ((proxy && proxy->filterImage(this, src, context, result, offset)) || this->onFilterImage(proxy, src, context, result, offset)) { - cache->set(this, *result, *offset); + if (externalCache) { + externalCache->set(this, *result, *offset); + } else if (context.cache()) { + context.cache()->set(key, *result, *offset); + } return true; } return false; @@ -439,3 +503,93 @@ CacheImpl::~CacheImpl() { delete v; } } + +namespace { + +class UniqueIDCacheImpl : public SkImageFilter::UniqueIDCache { +public: + UniqueIDCacheImpl(size_t maxBytes) : fMaxBytes(maxBytes), fCurrentBytes(0) { + } + virtual ~UniqueIDCacheImpl() { + SkTDynamicHash<Value, Key>::Iter iter(&fLookup); + + while (!iter.done()) { + Value* v = &*iter; + ++iter; + delete v; + } + } + struct Value { + Value(const Key& key, const SkBitmap& bitmap, const SkIPoint& offset) + : fKey(key), fBitmap(bitmap), fOffset(offset) {} + Key fKey; + SkBitmap fBitmap; + SkIPoint fOffset; + static const Key& GetKey(const Value& v) { + return v.fKey; + } + static uint32_t Hash(const Key& key) { + return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key), sizeof(Key)); + } + SK_DECLARE_INTERNAL_LLIST_INTERFACE(Value); + }; + virtual bool get(const Key& key, SkBitmap* result, SkIPoint* offset) const { + SkAutoMutexAcquire mutex(fMutex); + if (Value* v = fLookup.find(key)) { + *result = v->fBitmap; + *offset = v->fOffset; + if (v != fLRU.head()) { + fLRU.remove(v); + fLRU.addToHead(v); + } + return true; + } + return false; + } + virtual void set(const Key& key, const SkBitmap& result, const SkIPoint& offset) { + SkAutoMutexAcquire mutex(fMutex); + if (Value* v = fLookup.find(key)) { + removeInternal(v); + } + Value* v = new Value(key, result, offset); + fLookup.add(v); + fLRU.addToHead(v); + fCurrentBytes += result.getSize(); + while (fCurrentBytes > fMaxBytes) { + Value* tail = fLRU.tail(); + SkASSERT(tail); + if (tail == v) { + break; + } + removeInternal(tail); + } + } +private: + void removeInternal(Value* v) { + fCurrentBytes -= v->fBitmap.getSize(); + fLRU.remove(v); + fLookup.remove(v->fKey); + delete v; + } +private: + SkTDynamicHash<Value, Key> fLookup; + mutable SkTInternalLList<Value> fLRU; + size_t fMaxBytes; + size_t fCurrentBytes; + mutable SkMutex fMutex; +}; + +SkImageFilter::UniqueIDCache* CreateCache() { + return SkImageFilter::UniqueIDCache::Create(kDefaultCacheSize); +} + +} // namespace + +SkImageFilter::UniqueIDCache* SkImageFilter::UniqueIDCache::Create(size_t maxBytes) { + return SkNEW_ARGS(UniqueIDCacheImpl, (maxBytes)); +} + +SkImageFilter::UniqueIDCache* SkImageFilter::UniqueIDCache::Get() { + SK_DECLARE_STATIC_LAZY_PTR(SkImageFilter::UniqueIDCache, cache, CreateCache); + return cache.get(); +} diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp index 0abf9d84b5..2853805a5b 100644 --- a/src/gpu/SkGpuDevice.cpp +++ b/src/gpu/SkGpuDevice.cpp @@ -41,6 +41,8 @@ #include "SkXfermode.h" #include "SkErrorInternals.h" +enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 }; + #define CACHE_COMPATIBLE_DEVICE_TEXTURES 1 #if 0 @@ -1425,8 +1427,9 @@ void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap, SkMatrix matrix(*draw.fMatrix); matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top)); SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height()); - SkImageFilter::Cache* cache = SkImageFilter::Cache::Create(); - SkAutoUnref aur(cache); + SkAutoTUnref<SkImageFilter::UniqueIDCache> cache(getImageFilterCache()); + // This cache is transient, and is freed (along with all its contained + // textures) when it goes out of scope. SkImageFilter::Context ctx(matrix, clipBounds, cache); if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap, &offset)) { @@ -1535,8 +1538,9 @@ void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device, SkMatrix matrix(*draw.fMatrix); matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y)); SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height()); - SkImageFilter::Cache* cache = SkImageFilter::Cache::Create(); - SkAutoUnref aur(cache); + // This cache is transient, and is freed (along with all its contained + // textures) when it goes out of scope. + SkAutoTUnref<SkImageFilter::UniqueIDCache> cache(getImageFilterCache()); SkImageFilter::Context ctx(matrix, clipBounds, cache); if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap, &offset)) { @@ -2060,3 +2064,9 @@ bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* canvas, const SkPicture* pi return true; } + +SkImageFilter::UniqueIDCache* SkGpuDevice::getImageFilterCache() { + // We always return a transient cache, so it is freed after each + // filter traversal. + return SkImageFilter::UniqueIDCache::Create(kDefaultImageFilterCacheSize); +} diff --git a/tests/ImageFilterTest.cpp b/tests/ImageFilterTest.cpp index 7da4a911ef..0766c15edf 100644 --- a/tests/ImageFilterTest.cpp +++ b/tests/ImageFilterTest.cpp @@ -262,8 +262,7 @@ static void test_crop_rects(SkBaseDevice* device, skiatest::Reporter* reporter) SkIPoint offset; SkString str; str.printf("filter %d", static_cast<int>(i)); - SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(2)); - SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeLargest(), cache.get()); + SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeLargest(), NULL); REPORTER_ASSERT_MESSAGE(reporter, filter->filterImage(&proxy, bitmap, ctx, &result, &offset), str.c_str()); REPORTER_ASSERT_MESSAGE(reporter, offset.fX == 20 && offset.fY == 30, str.c_str()); |