Home | History | Annotate | Download | only in core
      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         SkASSERT(fMatrix.isFinite());   // otherwise we can't rely on == self when comparing keys
     32     }
     33 
     34     uint32_t fUniqueID;
     35     SkMatrix fMatrix;
     36     SkIRect fClipBounds;
     37     uint32_t fSrcGenID;
     38     SkIRect fSrcSubset;
     39 
     40     bool operator==(const SkImageFilterCacheKey& other) const {
     41         return fUniqueID == other.fUniqueID &&
     42                fMatrix == other.fMatrix &&
     43                fClipBounds == other.fClipBounds &&
     44                fSrcGenID == other.fSrcGenID &&
     45                fSrcSubset == other.fSrcSubset;
     46     }
     47 };
     48 
     49 // This cache maps from (filter's unique ID + CTM + clipBounds + src bitmap generation ID) to
     50 // (result, offset).
     51 class SkImageFilterCache : public SkRefCnt {
     52 public:
     53     enum { kDefaultTransientSize = 32 * 1024 * 1024 };
     54 
     55     virtual ~SkImageFilterCache() {}
     56     static SkImageFilterCache* Create(size_t maxBytes);
     57     static SkImageFilterCache* Get();
     58     virtual sk_sp<SkSpecialImage> get(const SkImageFilterCacheKey& key, SkIPoint* offset) const = 0;
     59     virtual void set(const SkImageFilterCacheKey& key, SkSpecialImage* image,
     60                      const SkIPoint& offset, const SkImageFilter* filter) = 0;
     61     virtual void purge() = 0;
     62     virtual void purgeByKeys(const SkImageFilterCacheKey[], int) = 0;
     63     SkDEBUGCODE(virtual int count() const = 0;)
     64 };
     65 
     66 #endif
     67