Debian Codegraph @

src/core/SkMaskFilterBase.cpp

raw
1 /*
2 * Copyright 2025 Google LLC
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 #include "src/core/SkMaskFilterBase.h"
8
9 #include "include/core/SkImageFilter.h"
10 #include "include/core/SkMatrix.h"
11 #include "include/core/SkPath.h"
12 #include "include/core/SkPoint.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkRegion.h"
16 #include "include/core/SkStrokeRec.h"
17 #include "include/core/SkTypes.h"
18 #include "include/private/base/SkTemplates.h"
19 #include "src/base/SkAutoMalloc.h"
20 #include "src/core/SkBlitter.h"
21 #include "src/core/SkCachedData.h"
22 #include "src/core/SkDraw.h"
23 #include "src/core/SkMask.h"
24 #include "src/core/SkPathPriv.h"
25 #include "src/core/SkRasterClip.h"
26
27 #include <algorithm>
28 #include <cstddef>
29 #include <cstdint>
30 #include <optional>
31
32 class SkPaint;
33 class SkRRect;
34
35 SkMaskFilterBase::NinePatch::~NinePatch() {
36 if (fCache) {
37 SkASSERT((const void*)fMask.fImage == fCache->data());
38 fCache->unref();
39 } else {
40 // fMask is about to be destroyed and "owns" its fImage.
41 SkMaskBuilder::FreeImage(const_cast<uint8_t*>(fMask.fImage));
42 }
43 }
44
45 bool SkMaskFilterBase::asABlur(BlurRec*) const {
46 return false;
47 }
48
49 std::pair<sk_sp<SkImageFilter>, bool> SkMaskFilterBase::asImageFilter(const SkMatrix& ctm,
50 const SkPaint& paint) const {
51 return std::make_pair(nullptr, false);
52 }
53
54 static SkMask extract_mask_subset(const SkMask& src, SkIRect bounds, int32_t newX, int32_t newY) {
55 SkASSERT(src.fBounds.contains(bounds));
56
57 const int dx = bounds.left() - src.fBounds.left();
58 const int dy = bounds.top() - src.fBounds.top();
59 bounds.offsetTo(newX, newY);
60 return SkMask(src.fImage + dy * src.fRowBytes + dx,
61 bounds,
62 src.fRowBytes,
63 src.fFormat);
64 }
65
66 static void blit_clipped_mask(SkBlitter* blitter, const SkMask& mask,
67 const SkIRect& bounds, const SkIRect& clipR) {
68 SkIRect r;
69 if (r.intersect(bounds, clipR)) {
70 blitter->blitMask(mask, r);
71 }
72 }
73
74 static void blit_clipped_rect(SkBlitter* blitter, const SkIRect& rect, const SkIRect& clipR) {
75 SkIRect r;
76 if (r.intersect(rect, clipR)) {
77 blitter->blitRect(r.left(), r.top(), r.width(), r.height());
78 }
79 }
80
81 static void draw_nine_clipped(const SkMask& mask, const SkIRect& outerR,
82 const SkIPoint& center, bool fillCenter,
83 const SkIRect& clipR, SkBlitter* blitter) {
84 const int cx = center.x();
85 const int cy = center.y();
86 SkIRect bounds;
87
88 // top-left
89 bounds = mask.fBounds;
90 bounds.fRight = cx;
91 bounds.fBottom = cy;
92 if (bounds.width() > 0 && bounds.height() > 0) {
93 SkMask m = extract_mask_subset(mask, bounds, outerR.left(), outerR.top());
94 blit_clipped_mask(blitter, m, m.fBounds, clipR);
95 }
96
97 // top-right
98 bounds = mask.fBounds;
99 bounds.fLeft = cx + 1;
100 bounds.fBottom = cy;
101 if (bounds.width() > 0 && bounds.height() > 0) {
102 SkMask m = extract_mask_subset(mask, bounds, outerR.right() - bounds.width(), outerR.top());
103 blit_clipped_mask(blitter, m, m.fBounds, clipR);
104 }
105
106 // bottom-left
107 bounds = mask.fBounds;
108 bounds.fRight = cx;
109 bounds.fTop = cy + 1;
110 if (bounds.width() > 0 && bounds.height() > 0) {
111 SkMask m = extract_mask_subset(mask, bounds, outerR.left(), outerR.bottom() - bounds.height());
112 blit_clipped_mask(blitter, m, m.fBounds, clipR);
113 }
114
115 // bottom-right
116 bounds = mask.fBounds;
117 bounds.fLeft = cx + 1;
118 bounds.fTop = cy + 1;
119 if (bounds.width() > 0 && bounds.height() > 0) {
120 SkMask m = extract_mask_subset(mask, bounds, outerR.right() - bounds.width(),
121 outerR.bottom() - bounds.height());
122 blit_clipped_mask(blitter, m, m.fBounds, clipR);
123 }
124
125 SkIRect innerR;
126 innerR.setLTRB(outerR.left() + cx - mask.fBounds.left(),
127 outerR.top() + cy - mask.fBounds.top(),
128 outerR.right() + (cx + 1 - mask.fBounds.right()),
129 outerR.bottom() + (cy + 1 - mask.fBounds.bottom()));
130 if (fillCenter) {
131 blit_clipped_rect(blitter, innerR, clipR);
132 }
133
134 const int innerW = innerR.width();
135 size_t storageSize = (innerW + 1) * (sizeof(int16_t) + sizeof(uint8_t));
136 SkAutoSMalloc<4*1024> storage(storageSize);
137 int16_t* runs = (int16_t*)storage.get();
138 uint8_t* alpha = (uint8_t*)(runs + innerW + 1);
139
140 SkIRect r;
141 // top
142 r.setLTRB(innerR.left(), outerR.top(), innerR.right(), innerR.top());
143 if (r.intersect(clipR)) {
144 const int startY = std::max(0, r.top() - outerR.top());
145 const int stopY = startY + r.height();
146 const int width = r.width();
147 runs[0] = width;
148 runs[width] = 0;
149 for (int y = startY; y < stopY; ++y) {
150 alpha[0] = *mask.getAddr8(cx, mask.fBounds.top() + y);
151 blitter->blitAntiH(r.left(), outerR.top() + y, alpha, runs);
152 }
153 }
154 // bottom
155 r.setLTRB(innerR.left(), innerR.bottom(), innerR.right(), outerR.bottom());
156 if (r.intersect(clipR)) {
157 const int startY = outerR.bottom() - r.bottom();
158 const int stopY = startY + r.height();
159 const int width = r.width();
160 runs[0] = width;
161 runs[width] = 0;
162 for (int y = startY; y < stopY; ++y) {
163 alpha[0] = *mask.getAddr8(cx, mask.fBounds.bottom() - y - 1);
164 blitter->blitAntiH(r.left(), outerR.bottom() - y - 1, alpha, runs);
165 }
166 }
167 // left
168 r.setLTRB(outerR.left(), innerR.top(), innerR.left(), innerR.bottom());
169 if (r.intersect(clipR)) {
170 SkMask leftMask(mask.getAddr8(mask.fBounds.left() + r.left() - outerR.left(),
171 mask.fBounds.top() + cy),
172 r,
173 0, // so we repeat the scanline for our height
174 SkMask::kA8_Format);
175 blitter->blitMask(leftMask, r);
176 }
177 // right
178 r.setLTRB(innerR.right(), innerR.top(), outerR.right(), innerR.bottom());
179 if (r.intersect(clipR)) {
180 SkMask rightMask(mask.getAddr8(mask.fBounds.right() - outerR.right() + r.left(),
181 mask.fBounds.top() + cy),
182 r,
183 0, // so we repeat the scanline for our height
184 SkMask::kA8_Format);
185 blitter->blitMask(rightMask, r);
186 }
187 }
188
189 static void draw_nine(const SkMask& mask, const SkIRect& outerR, const SkIPoint& center,
190 bool fillCenter, const SkRasterClip& clip, SkBlitter* blitter) {
191 // if we get here, we need to (possibly) resolve the clip and blitter
192 SkAAClipBlitterWrapper wrapper(clip, blitter);
193 blitter = wrapper.getBlitter();
194
195 SkRegion::Cliperator clipper(wrapper.getRgn(), outerR);
196
197 if (!clipper.done()) {
198 const SkIRect& cr = clipper.rect();
199 do {
200 draw_nine_clipped(mask, outerR, center, fillCenter, cr, blitter);
201 clipper.next();
202 } while (!clipper.done());
203 }
204 }
205
206 static int countNestedRects(const SkPathRaw& raw, SkRect rects[2]) {
207 if (SkPathPriv::IsNestedFillRects(raw, rects)) {
208 return 2;
209 }
210 if (auto r = raw.isRect()) {
211 rects[0] = *r;
212 return 1;
213 }
214 return 0;
215 }
216
217 bool SkMaskFilterBase::filterRRect(const SkRRect& devRRect,
218 const SkMatrix& matrix,
219 const SkRasterClip& clip,
220 SkBlitter* blitter,
221 SkResourceCache* cache) const {
222 // Attempt to speed up drawing by creating a nine patch. If a nine patch
223 // cannot be used, return false to allow our caller to recover and perform
224 // the drawing another way.
225 std::optional<NinePatch> patch =
226 this->filterRRectToNine(devRRect, matrix, clip.getBounds(), cache);
227
228 if (!patch.has_value()) {
229 return false;
230 }
231 draw_nine(patch->fMask, patch->fOuterRect, patch->fCenter, true, clip, blitter);
232 return true;
233 }
234
235 SkMaskFilterBase::FilterReturn SkMaskFilterBase::filterRects(SkSpan<const SkRect> devRects,
236 const SkMatrix& matrix,
237 const SkRasterClip& clip,
238 SkBlitter* blitter,
239 SkResourceCache* cache) const {
240 std::optional<NinePatch> patch;
241
242 FilterReturn filterReturn = this->filterRectsToNine(
243 devRects, matrix, clip.getBounds(), &patch, cache);
244 switch (filterReturn) {
245 case FilterReturn::kFalse:
246 SkASSERT(!patch.has_value());
247 break;
248
249 case FilterReturn::kTrue:
250 draw_nine(patch->fMask, patch->fOuterRect, patch->fCenter, 1 == devRects.size(), clip,
251 blitter);
252 break;
253
254 case FilterReturn::kUnimplemented:
255 SkASSERT(!patch.has_value());
256 // fall out
257 break;
258 }
259 return filterReturn;
260 }
261
262 bool SkMaskFilterBase::filterPath(const SkPathRaw& devRaw,
263 const SkMatrix& matrix,
264 const SkRasterClip& clip,
265 SkBlitter* blitter,
266 SkStrokeRec::InitStyle style,
267 SkResourceCache* cache) const {
268 SkRect rects[2];
269 int rectCount = 0;
270 if (SkStrokeRec::kFill_InitStyle == style) {
271 rectCount = countNestedRects(devRaw, rects);
272 }
273 if (rectCount > 0) {
274 switch (this->filterRects(SkSpan(rects, rectCount), matrix, clip, blitter, cache)) {
275 case FilterReturn::kFalse:
276 return false;
277 case FilterReturn::kTrue:
278 return true;
279 case FilterReturn::kUnimplemented:
280 break;
281 }
282 }
283
284 SkMaskBuilder srcM, dstM;
285
286 #if defined(SK_BUILD_FOR_FUZZER)
287 if (devRaw.verbs().size() > 1000 || devRaw.points().size() > 1000) {
288 return false;
289 }
290 #endif
291 if (!skcpu::DrawToMask(devRaw,
292 clip.getBounds(),
293 this,
294 &matrix,
295 &srcM,
296 SkMaskBuilder::kComputeBoundsAndRenderImage_CreateMode,
297 style)) {
298 return false;
299 }
300 SkAutoMaskFreeImage autoSrc(srcM.image());
301
302 if (!this->filterMask(&dstM, srcM, matrix, nullptr)) {
303 return false;
304 }
305 SkAutoMaskFreeImage autoDst(dstM.image());
306
307 // if we get here, we need to (possibly) resolve the clip and blitter
308 SkAAClipBlitterWrapper wrapper(clip, blitter);
309 blitter = wrapper.getBlitter();
310
311 SkRegion::Cliperator clipper(wrapper.getRgn(), dstM.fBounds);
312
313 if (!clipper.done()) {
314 const SkIRect& cr = clipper.rect();
315 do {
316 blitter->blitMask(dstM, cr);
317 clipper.next();
318 } while (!clipper.done());
319 }
320
321 return true;
322 }
323
324 std::optional<SkMaskFilterBase::NinePatch> SkMaskFilterBase::filterRRectToNine(
325 const SkRRect&, const SkMatrix&, const SkIRect&, SkResourceCache*) const {
326 return std::nullopt;
327 }
328
329 SkMaskFilterBase::FilterReturn SkMaskFilterBase::filterRectsToNine(SkSpan<const SkRect>,
330 const SkMatrix&,
331 const SkIRect&,
332 std::optional<NinePatch>*,
333 SkResourceCache*) const {
334 return FilterReturn::kUnimplemented;
335 }
336
337 void SkMaskFilterBase::computeFastBounds(const SkRect& src, SkRect* dst) const {
338 SkMask srcM(nullptr, src.roundOut(), 0, SkMask::kA8_Format);
339 SkMaskBuilder dstM;
340
341 SkIPoint margin; // ignored
342 if (this->filterMask(&dstM, srcM, SkMatrix::I(), &margin)) {
343 dst->set(dstM.fBounds);
344 } else {
345 dst->set(srcM.fBounds);
346 }
347 }