SkImageFilterCache.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #ifndef SkImageFilterCache_DEFINED
  8. #define SkImageFilterCache_DEFINED
  9. #include "include/core/SkMatrix.h"
  10. #include "include/core/SkRefCnt.h"
  11. struct SkIPoint;
  12. class SkImageFilter;
  13. class SkSpecialImage;
  14. struct SkImageFilterCacheKey {
  15. SkImageFilterCacheKey(const uint32_t uniqueID, const SkMatrix& matrix,
  16. const SkIRect& clipBounds, uint32_t srcGenID, const SkIRect& srcSubset)
  17. : fUniqueID(uniqueID)
  18. , fMatrix(matrix)
  19. , fClipBounds(clipBounds)
  20. , fSrcGenID(srcGenID)
  21. , fSrcSubset(srcSubset) {
  22. // Assert that Key is tightly-packed, since it is hashed.
  23. static_assert(sizeof(SkImageFilterCacheKey) == sizeof(uint32_t) + sizeof(SkMatrix) +
  24. sizeof(SkIRect) + sizeof(uint32_t) + 4 * sizeof(int32_t),
  25. "image_filter_key_tight_packing");
  26. fMatrix.getType(); // force initialization of type, so hashes match
  27. SkASSERT(fMatrix.isFinite()); // otherwise we can't rely on == self when comparing keys
  28. }
  29. uint32_t fUniqueID;
  30. SkMatrix fMatrix;
  31. SkIRect fClipBounds;
  32. uint32_t fSrcGenID;
  33. SkIRect fSrcSubset;
  34. bool operator==(const SkImageFilterCacheKey& other) const {
  35. return fUniqueID == other.fUniqueID &&
  36. fMatrix == other.fMatrix &&
  37. fClipBounds == other.fClipBounds &&
  38. fSrcGenID == other.fSrcGenID &&
  39. fSrcSubset == other.fSrcSubset;
  40. }
  41. };
  42. // This cache maps from (filter's unique ID + CTM + clipBounds + src bitmap generation ID) to
  43. // (result, offset).
  44. class SkImageFilterCache : public SkRefCnt {
  45. public:
  46. enum { kDefaultTransientSize = 32 * 1024 * 1024 };
  47. virtual ~SkImageFilterCache() {}
  48. static SkImageFilterCache* Create(size_t maxBytes);
  49. static SkImageFilterCache* Get();
  50. virtual sk_sp<SkSpecialImage> get(const SkImageFilterCacheKey& key, SkIPoint* offset) const = 0;
  51. virtual void set(const SkImageFilterCacheKey& key, SkSpecialImage* image,
  52. const SkIPoint& offset, const SkImageFilter* filter) = 0;
  53. virtual void purge() = 0;
  54. virtual void purgeByImageFilter(const SkImageFilter*) = 0;
  55. SkDEBUGCODE(virtual int count() const = 0;)
  56. };
  57. #endif