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