1 /* 2 * Copyright 2016 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef SkImageFilterCache_DEFINED 9 #define SkImageFilterCache_DEFINED 10 11 #include "SkMatrix.h" 12 #include "SkRefCnt.h" 13 14 struct SkIPoint; 15 class SkImageFilter; 16 class SkSpecialImage; 17 18 struct SkImageFilterCacheKey { 19 SkImageFilterCacheKey(const uint32_t uniqueID, const SkMatrix& matrix, 20 const SkIRect& clipBounds, uint32_t srcGenID, const SkIRect& srcSubset) 21 : fUniqueID(uniqueID) 22 , fMatrix(matrix) 23 , fClipBounds(clipBounds) 24 , fSrcGenID(srcGenID) 25 , fSrcSubset(srcSubset) { 26 // Assert that Key is tightly-packed, since it is hashed. 27 static_assert(sizeof(SkImageFilterCacheKey) == sizeof(uint32_t) + sizeof(SkMatrix) + 28 sizeof(SkIRect) + sizeof(uint32_t) + 4 * sizeof(int32_t), 29 "image_filter_key_tight_packing"); 30 fMatrix.getType(); // force initialization of type, so hashes match 31 } 32 33 uint32_t fUniqueID; 34 SkMatrix fMatrix; 35 SkIRect fClipBounds; 36 uint32_t fSrcGenID; 37 SkIRect fSrcSubset; 38 39 bool operator==(const SkImageFilterCacheKey& other) const { 40 return fUniqueID == other.fUniqueID && 41 fMatrix == other.fMatrix && 42 fClipBounds == other.fClipBounds && 43 fSrcGenID == other.fSrcGenID && 44 fSrcSubset == other.fSrcSubset; 45 } 46 }; 47 48 // This cache maps from (filter's unique ID + CTM + clipBounds + src bitmap generation ID) to 49 // (result, offset). 50 class SkImageFilterCache : public SkRefCnt { 51 public: 52 enum { kDefaultTransientSize = 32 * 1024 * 1024 }; 53 54 virtual ~SkImageFilterCache() {} 55 static SkImageFilterCache* Create(size_t maxBytes); 56 static SkImageFilterCache* Get(); 57 virtual sk_sp<SkSpecialImage> get(const SkImageFilterCacheKey& key, SkIPoint* offset) const = 0; 58 virtual void set(const SkImageFilterCacheKey& key, SkSpecialImage* image, 59 const SkIPoint& offset, const SkImageFilter* filter) = 0; 60 virtual void purge() = 0; 61 virtual void purgeByKeys(const SkImageFilterCacheKey[], int) = 0; 62 SkDEBUGCODE(virtual int count() const = 0;) 63 }; 64 65 #endif 66