Debian Codegraph @

src/core/SkMaskCache.cpp

raw
1 /*
2 * Copyright 2014 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 #include "src/core/SkMaskCache.h"
9
10 #include "include/core/SkRRect.h"
11 #include "include/core/SkRect.h"
12 #include "include/core/SkSize.h"
13 #include "include/private/base/SkAssert.h"
14 #include "src/base/SkTLazy.h"
15 #include "src/core/SkCachedData.h"
16 #include "src/core/SkMask.h"
17 #include "src/core/SkResourceCache.h"
18
19 #include <cstddef>
20 #include <cstdint>
21
22 class SkDiscardableMemory;
23 enum SkBlurStyle : int;
24
25 #define CHECK_LOCAL(localCache, localName, globalName, ...) \
26 ((localCache) ? localCache->localName(__VA_ARGS__) : SkResourceCache::globalName(__VA_ARGS__))
27
28 struct MaskValue {
29 SkMask fMask;
30 SkCachedData* fData;
31 };
32
33 namespace {
34 static unsigned gRRectBlurKeyNamespaceLabel;
35
36 struct RRectBlurKey : public SkResourceCache::Key {
37 public:
38 RRectBlurKey(SkScalar sigma, const SkRRect& rrect, SkBlurStyle style)
39 : fSigma(sigma)
40 , fStyle(style)
41 , fRRect(rrect)
42 {
43 this->init(&gRRectBlurKeyNamespaceLabel, 0,
44 sizeof(fSigma) + sizeof(fStyle) + sizeof(fRRect));
45 }
46
47 SkScalar fSigma;
48 int32_t fStyle;
49 SkRRect fRRect;
50 };
51
52 struct RRectBlurRec : public SkResourceCache::Rec {
53 RRectBlurRec(RRectBlurKey key, const SkMask& mask, SkCachedData* data)
54 : fKey(key), fValue({{nullptr, mask.fBounds, mask.fRowBytes, mask.fFormat}, data})
55 {
56 fValue.fData->attachToCacheAndRef();
57 }
58 ~RRectBlurRec() override {
59 fValue.fData->detachFromCacheAndUnref();
60 }
61
62 RRectBlurKey fKey;
63 MaskValue fValue;
64
65 const Key& getKey() const override { return fKey; }
66 size_t bytesUsed() const override { return sizeof(*this) + fValue.fData->size(); }
67 const char* getCategory() const override { return "rrect-blur"; }
68 SkDiscardableMemory* diagnostic_only_getDiscardable() const override {
69 return fValue.fData->diagnostic_only_getDiscardable();
70 }
71
72 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextData) {
73 const RRectBlurRec& rec = static_cast<const RRectBlurRec&>(baseRec);
74 SkTLazy<MaskValue>* result = (SkTLazy<MaskValue>*)contextData;
75
76 SkCachedData* tmpData = rec.fValue.fData;
77 tmpData->ref();
78 if (nullptr == tmpData->data()) {
79 tmpData->unref();
80 return false;
81 }
82 result->init(rec.fValue);
83 return true;
84 }
85 };
86 } // namespace
87
88 SkCachedData* SkMaskCache::FindAndRef(SkScalar sigma, SkBlurStyle style,
89 const SkRRect& rrect, SkTLazy<SkMask>* mask,
90 SkResourceCache* localCache) {
91 SkTLazy<MaskValue> result;
92 RRectBlurKey key(sigma, rrect, style);
93 if (!CHECK_LOCAL(localCache, find, Find, key, RRectBlurRec::Visitor, &result)) {
94 return nullptr;
95 }
96
97 mask->init(static_cast<const uint8_t*>(result->fData->data()),
98 result->fMask.fBounds, result->fMask.fRowBytes, result->fMask.fFormat);
99 return result->fData;
100 }
101
102 void SkMaskCache::Add(SkScalar sigma, SkBlurStyle style,
103 const SkRRect& rrect, const SkMask& mask, SkCachedData* data,
104 SkResourceCache* localCache) {
105 RRectBlurKey key(sigma, rrect, style);
106 return CHECK_LOCAL(localCache, add, Add, new RRectBlurRec(key, mask, data));
107 }
108
109 //////////////////////////////////////////////////////////////////////////////////////////
110
111 namespace {
112 static unsigned gRectsBlurKeyNamespaceLabel;
113
114 struct RectsBlurKey : public SkResourceCache::Key {
115 public:
116 RectsBlurKey(SkScalar sigma, SkBlurStyle style, SkSpan<const SkRect> rects)
117 : fSigma(sigma), fStyle(style) {
118 SkASSERT(rects.size() == 1 || rects.size() == 2);
119 SkIRect ir;
120 rects[0].roundOut(&ir);
121 fSizes[0] = SkSize{rects[0].width(), rects[0].height()};
122 if (rects.size() == 2) {
123 fSizes[1] = SkSize{rects[1].width(), rects[1].height()};
124 fSizes[2] = SkSize{rects[0].x() - rects[1].x(), rects[0].y() - rects[1].y()};
125 } else {
126 fSizes[1] = SkSize{0, 0};
127 fSizes[2] = SkSize{0, 0};
128 }
129 fSizes[3] = SkSize{rects[0].x() - ir.x(), rects[0].y() - ir.y()};
130
131 this->init(&gRectsBlurKeyNamespaceLabel, 0,
132 sizeof(fSigma) + sizeof(fStyle) + sizeof(fSizes));
133 }
134
135 SkScalar fSigma;
136 int32_t fStyle;
137 SkSize fSizes[4];
138 };
139
140 struct RectsBlurRec : public SkResourceCache::Rec {
141 RectsBlurRec(RectsBlurKey key, const SkMask& mask, SkCachedData* data)
142 : fKey(key), fValue({{nullptr, mask.fBounds, mask.fRowBytes, mask.fFormat}, data})
143 {
144 fValue.fData->attachToCacheAndRef();
145 }
146 ~RectsBlurRec() override {
147 fValue.fData->detachFromCacheAndUnref();
148 }
149
150 RectsBlurKey fKey;
151 MaskValue fValue;
152
153 const Key& getKey() const override { return fKey; }
154 size_t bytesUsed() const override { return sizeof(*this) + fValue.fData->size(); }
155 const char* getCategory() const override { return "rects-blur"; }
156 SkDiscardableMemory* diagnostic_only_getDiscardable() const override {
157 return fValue.fData->diagnostic_only_getDiscardable();
158 }
159
160 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextData) {
161 const RectsBlurRec& rec = static_cast<const RectsBlurRec&>(baseRec);
162 SkTLazy<MaskValue>* result = static_cast<SkTLazy<MaskValue>*>(contextData);
163
164 SkCachedData* tmpData = rec.fValue.fData;
165 tmpData->ref();
166 if (nullptr == tmpData->data()) {
167 tmpData->unref();
168 return false;
169 }
170 result->init(rec.fValue);
171 return true;
172 }
173 };
174 } // namespace
175
176 SkCachedData* SkMaskCache::FindAndRef(SkScalar sigma,
177 SkBlurStyle style,
178 SkSpan<const SkRect> rects,
179 SkTLazy<SkMask>* mask,
180 SkResourceCache* localCache) {
181 SkTLazy<MaskValue> result;
182 RectsBlurKey key(sigma, style, rects);
183 if (!CHECK_LOCAL(localCache, find, Find, key, RectsBlurRec::Visitor, &result)) {
184 return nullptr;
185 }
186
187 mask->init(static_cast<const uint8_t*>(result->fData->data()),
188 result->fMask.fBounds, result->fMask.fRowBytes, result->fMask.fFormat);
189 return result->fData;
190 }
191
192 void SkMaskCache::Add(SkScalar sigma,
193 SkBlurStyle style,
194 SkSpan<const SkRect> rects,
195 const SkMask& mask,
196 SkCachedData* data,
197 SkResourceCache* localCache) {
198 RectsBlurKey key(sigma, style, rects);
199 return CHECK_LOCAL(localCache, add, Add, new RectsBlurRec(key, mask, data));
200 }