1 /*
2 * Copyright 2012 The Android Open Source Project
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 "include/core/SkBitmap.h"
9 #include "include/core/SkRect.h"
10 #include "include/core/SkTileMode.h"
11 #include "include/core/SkUnPreMultiply.h"
12 #include "include/effects/SkImageFilters.h"
13 #include "include/private/SkColorData.h"
14 #include "include/private/SkTPin.h"
15 #include "src/core/SkImageFilter_Base.h"
16 #include "src/core/SkReadBuffer.h"
17 #include "src/core/SkSpecialImage.h"
18 #include "src/core/SkWriteBuffer.h"
19 #include "src/gpu/SkGr.h"
20
21 #if SK_SUPPORT_GPU
22 #include "src/gpu/GrRecordingContextPriv.h"
23 #include "src/gpu/GrTextureProxy.h"
24 #include "src/gpu/effects/GrMatrixConvolutionEffect.h"
25 #endif
26
27 namespace {
28
29 class SkMatrixConvolutionImageFilter final : public SkImageFilter_Base {
30 public:
SkMatrixConvolutionImageFilter(const SkISize & kernelSize,const SkScalar * kernel,SkScalar gain,SkScalar bias,const SkIPoint & kernelOffset,SkTileMode tileMode,bool convolveAlpha,sk_sp<SkImageFilter> input,const SkRect * cropRect)31 SkMatrixConvolutionImageFilter(const SkISize& kernelSize, const SkScalar* kernel,
32 SkScalar gain, SkScalar bias, const SkIPoint& kernelOffset,
33 SkTileMode tileMode, bool convolveAlpha,
34 sk_sp<SkImageFilter> input, const SkRect* cropRect)
35 : INHERITED(&input, 1, cropRect)
36 , fKernelSize(kernelSize)
37 , fGain(gain)
38 , fBias(bias)
39 , fKernelOffset(kernelOffset)
40 , fTileMode(tileMode)
41 , fConvolveAlpha(convolveAlpha) {
42 size_t size = (size_t) sk_64_mul(fKernelSize.width(), fKernelSize.height());
43 fKernel = new SkScalar[size];
44 memcpy(fKernel, kernel, size * sizeof(SkScalar));
45 SkASSERT(kernelSize.fWidth >= 1 && kernelSize.fHeight >= 1);
46 SkASSERT(kernelOffset.fX >= 0 && kernelOffset.fX < kernelSize.fWidth);
47 SkASSERT(kernelOffset.fY >= 0 && kernelOffset.fY < kernelSize.fHeight);
48 }
49
~SkMatrixConvolutionImageFilter()50 ~SkMatrixConvolutionImageFilter() override {
51 delete[] fKernel;
52 }
53
54 protected:
55
56 void flatten(SkWriteBuffer&) const override;
57
58 sk_sp<SkSpecialImage> onFilterImage(const Context&, SkIPoint* offset) const override;
59 SkIRect onFilterNodeBounds(const SkIRect&, const SkMatrix& ctm,
60 MapDirection, const SkIRect* inputRect) const override;
61 bool affectsTransparentBlack() const override;
62
63 private:
64 friend void ::SkRegisterMatrixConvolutionImageFilterFlattenable();
65 SK_FLATTENABLE_HOOKS(SkMatrixConvolutionImageFilter)
66
67 SkISize fKernelSize;
68 SkScalar* fKernel;
69 SkScalar fGain;
70 SkScalar fBias;
71 SkIPoint fKernelOffset;
72 SkTileMode fTileMode;
73 bool fConvolveAlpha;
74
75 template <class PixelFetcher, bool convolveAlpha>
76 void filterPixels(const SkBitmap& src,
77 SkBitmap* result,
78 SkIVector& offset,
79 const SkIRect& rect,
80 const SkIRect& bounds) const;
81 template <class PixelFetcher>
82 void filterPixels(const SkBitmap& src,
83 SkBitmap* result,
84 SkIVector& offset,
85 const SkIRect& rect,
86 const SkIRect& bounds) const;
87 void filterInteriorPixels(const SkBitmap& src,
88 SkBitmap* result,
89 SkIVector& offset,
90 const SkIRect& rect,
91 const SkIRect& bounds) const;
92 void filterBorderPixels(const SkBitmap& src,
93 SkBitmap* result,
94 SkIVector& offset,
95 const SkIRect& rect,
96 const SkIRect& bounds) const;
97
98 using INHERITED = SkImageFilter_Base;
99 };
100
101 class UncheckedPixelFetcher {
102 public:
fetch(const SkBitmap & src,int x,int y,const SkIRect & bounds)103 static inline SkPMColor fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) {
104 return *src.getAddr32(x, y);
105 }
106 };
107
108 class ClampPixelFetcher {
109 public:
fetch(const SkBitmap & src,int x,int y,const SkIRect & bounds)110 static inline SkPMColor fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) {
111 x = SkTPin(x, bounds.fLeft, bounds.fRight - 1);
112 y = SkTPin(y, bounds.fTop, bounds.fBottom - 1);
113 return *src.getAddr32(x, y);
114 }
115 };
116
117 class RepeatPixelFetcher {
118 public:
fetch(const SkBitmap & src,int x,int y,const SkIRect & bounds)119 static inline SkPMColor fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) {
120 x = (x - bounds.left()) % bounds.width() + bounds.left();
121 y = (y - bounds.top()) % bounds.height() + bounds.top();
122 if (x < bounds.left()) {
123 x += bounds.width();
124 }
125 if (y < bounds.top()) {
126 y += bounds.height();
127 }
128 return *src.getAddr32(x, y);
129 }
130 };
131
132 class ClampToBlackPixelFetcher {
133 public:
fetch(const SkBitmap & src,int x,int y,const SkIRect & bounds)134 static inline SkPMColor fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) {
135 if (x < bounds.fLeft || x >= bounds.fRight || y < bounds.fTop || y >= bounds.fBottom) {
136 return 0;
137 } else {
138 return *src.getAddr32(x, y);
139 }
140 }
141 };
142
143 } // end namespace
144
MatrixConvolution(const SkISize & kernelSize,const SkScalar kernel[],SkScalar gain,SkScalar bias,const SkIPoint & kernelOffset,SkTileMode tileMode,bool convolveAlpha,sk_sp<SkImageFilter> input,const CropRect & cropRect)145 sk_sp<SkImageFilter> SkImageFilters::MatrixConvolution(const SkISize& kernelSize,
146 const SkScalar kernel[],
147 SkScalar gain,
148 SkScalar bias,
149 const SkIPoint& kernelOffset,
150 SkTileMode tileMode,
151 bool convolveAlpha,
152 sk_sp<SkImageFilter> input,
153 const CropRect& cropRect) {
154 // We need to be able to read at most SK_MaxS32 bytes, so divide that
155 // by the size of a scalar to know how many scalars we can read.
156 static constexpr int32_t kMaxKernelSize = SK_MaxS32 / sizeof(SkScalar);
157
158 if (kernelSize.width() < 1 || kernelSize.height() < 1) {
159 return nullptr;
160 }
161 if (kMaxKernelSize / kernelSize.fWidth < kernelSize.fHeight) {
162 return nullptr;
163 }
164 if (!kernel) {
165 return nullptr;
166 }
167 if ((kernelOffset.fX < 0) || (kernelOffset.fX >= kernelSize.fWidth) ||
168 (kernelOffset.fY < 0) || (kernelOffset.fY >= kernelSize.fHeight)) {
169 return nullptr;
170 }
171 return sk_sp<SkImageFilter>(new SkMatrixConvolutionImageFilter(
172 kernelSize, kernel, gain, bias, kernelOffset, tileMode, convolveAlpha,
173 std::move(input), cropRect));
174 }
175
SkRegisterMatrixConvolutionImageFilterFlattenable()176 void SkRegisterMatrixConvolutionImageFilterFlattenable() {
177 SK_REGISTER_FLATTENABLE(SkMatrixConvolutionImageFilter);
178 // TODO (michaelludwig) - Remove after grace period for SKPs to stop using old name
179 SkFlattenable::Register("SkMatrixConvolutionImageFilterImpl",
180 SkMatrixConvolutionImageFilter::CreateProc);
181 }
182
CreateProc(SkReadBuffer & buffer)183 sk_sp<SkFlattenable> SkMatrixConvolutionImageFilter::CreateProc(SkReadBuffer& buffer) {
184 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
185
186 SkISize kernelSize;
187 kernelSize.fWidth = buffer.readInt();
188 kernelSize.fHeight = buffer.readInt();
189 const int count = buffer.getArrayCount();
190
191 const int64_t kernelArea = sk_64_mul(kernelSize.width(), kernelSize.height());
192 if (!buffer.validate(kernelArea == count)) {
193 return nullptr;
194 }
195 if (!buffer.validateCanReadN<SkScalar>(count)) {
196 return nullptr;
197 }
198 SkAutoSTArray<16, SkScalar> kernel(count);
199 if (!buffer.readScalarArray(kernel.get(), count)) {
200 return nullptr;
201 }
202 SkScalar gain = buffer.readScalar();
203 SkScalar bias = buffer.readScalar();
204 SkIPoint kernelOffset;
205 kernelOffset.fX = buffer.readInt();
206 kernelOffset.fY = buffer.readInt();
207
208 SkTileMode tileMode = buffer.read32LE(SkTileMode::kLastTileMode);
209 bool convolveAlpha = buffer.readBool();
210
211 if (!buffer.isValid()) {
212 return nullptr;
213 }
214 return SkImageFilters::MatrixConvolution(
215 kernelSize, kernel.get(), gain, bias, kernelOffset, tileMode,
216 convolveAlpha, common.getInput(0), common.cropRect());
217 }
218
flatten(SkWriteBuffer & buffer) const219 void SkMatrixConvolutionImageFilter::flatten(SkWriteBuffer& buffer) const {
220 this->INHERITED::flatten(buffer);
221 buffer.writeInt(fKernelSize.fWidth);
222 buffer.writeInt(fKernelSize.fHeight);
223 buffer.writeScalarArray(fKernel, fKernelSize.fWidth * fKernelSize.fHeight);
224 buffer.writeScalar(fGain);
225 buffer.writeScalar(fBias);
226 buffer.writeInt(fKernelOffset.fX);
227 buffer.writeInt(fKernelOffset.fY);
228 buffer.writeInt((int) fTileMode);
229 buffer.writeBool(fConvolveAlpha);
230 }
231
232 ///////////////////////////////////////////////////////////////////////////////////////////////////
233
234 template<class PixelFetcher, bool convolveAlpha>
filterPixels(const SkBitmap & src,SkBitmap * result,SkIVector & offset,const SkIRect & r,const SkIRect & bounds) const235 void SkMatrixConvolutionImageFilter::filterPixels(const SkBitmap& src,
236 SkBitmap* result,
237 SkIVector& offset,
238 const SkIRect& r,
239 const SkIRect& bounds) const {
240 SkIRect rect(r);
241 if (!rect.intersect(bounds)) {
242 return;
243 }
244 for (int y = rect.fTop; y < rect.fBottom; ++y) {
245 SkPMColor* dptr = result->getAddr32(rect.fLeft - offset.fX, y - offset.fY);
246 for (int x = rect.fLeft; x < rect.fRight; ++x) {
247 SkScalar sumA = 0, sumR = 0, sumG = 0, sumB = 0;
248 for (int cy = 0; cy < fKernelSize.fHeight; cy++) {
249 for (int cx = 0; cx < fKernelSize.fWidth; cx++) {
250 SkPMColor s = PixelFetcher::fetch(src,
251 x + cx - fKernelOffset.fX,
252 y + cy - fKernelOffset.fY,
253 bounds);
254 SkScalar k = fKernel[cy * fKernelSize.fWidth + cx];
255 if (convolveAlpha) {
256 sumA += SkGetPackedA32(s) * k;
257 }
258 sumR += SkGetPackedR32(s) * k;
259 sumG += SkGetPackedG32(s) * k;
260 sumB += SkGetPackedB32(s) * k;
261 }
262 }
263 int a = convolveAlpha
264 ? SkTPin(SkScalarFloorToInt(sumA * fGain + fBias), 0, 255)
265 : 255;
266 int r = SkTPin(SkScalarFloorToInt(sumR * fGain + fBias), 0, a);
267 int g = SkTPin(SkScalarFloorToInt(sumG * fGain + fBias), 0, a);
268 int b = SkTPin(SkScalarFloorToInt(sumB * fGain + fBias), 0, a);
269 if (!convolveAlpha) {
270 a = SkGetPackedA32(PixelFetcher::fetch(src, x, y, bounds));
271 *dptr++ = SkPreMultiplyARGB(a, r, g, b);
272 } else {
273 *dptr++ = SkPackARGB32(a, r, g, b);
274 }
275 }
276 }
277 }
278
279 template<class PixelFetcher>
filterPixels(const SkBitmap & src,SkBitmap * result,SkIVector & offset,const SkIRect & rect,const SkIRect & bounds) const280 void SkMatrixConvolutionImageFilter::filterPixels(const SkBitmap& src,
281 SkBitmap* result,
282 SkIVector& offset,
283 const SkIRect& rect,
284 const SkIRect& bounds) const {
285 if (fConvolveAlpha) {
286 filterPixels<PixelFetcher, true>(src, result, offset, rect, bounds);
287 } else {
288 filterPixels<PixelFetcher, false>(src, result, offset, rect, bounds);
289 }
290 }
291
filterInteriorPixels(const SkBitmap & src,SkBitmap * result,SkIVector & offset,const SkIRect & rect,const SkIRect & bounds) const292 void SkMatrixConvolutionImageFilter::filterInteriorPixels(const SkBitmap& src,
293 SkBitmap* result,
294 SkIVector& offset,
295 const SkIRect& rect,
296 const SkIRect& bounds) const {
297 switch (fTileMode) {
298 case SkTileMode::kMirror:
299 // TODO (michaelludwig) - Implement mirror tiling, treat as repeat for now.
300 case SkTileMode::kRepeat:
301 // In repeat mode, we still need to wrap the samples around the src
302 filterPixels<RepeatPixelFetcher>(src, result, offset, rect, bounds);
303 break;
304 case SkTileMode::kClamp:
305 // Fall through
306 case SkTileMode::kDecal:
307 filterPixels<UncheckedPixelFetcher>(src, result, offset, rect, bounds);
308 break;
309 }
310 }
311
filterBorderPixels(const SkBitmap & src,SkBitmap * result,SkIVector & offset,const SkIRect & rect,const SkIRect & srcBounds) const312 void SkMatrixConvolutionImageFilter::filterBorderPixels(const SkBitmap& src,
313 SkBitmap* result,
314 SkIVector& offset,
315 const SkIRect& rect,
316 const SkIRect& srcBounds) const {
317 switch (fTileMode) {
318 case SkTileMode::kClamp:
319 filterPixels<ClampPixelFetcher>(src, result, offset, rect, srcBounds);
320 break;
321 case SkTileMode::kMirror:
322 // TODO (michaelludwig) - Implement mirror tiling, treat as repeat for now.
323 case SkTileMode::kRepeat:
324 filterPixels<RepeatPixelFetcher>(src, result, offset, rect, srcBounds);
325 break;
326 case SkTileMode::kDecal:
327 filterPixels<ClampToBlackPixelFetcher>(src, result, offset, rect, srcBounds);
328 break;
329 }
330 }
331
onFilterImage(const Context & ctx,SkIPoint * offset) const332 sk_sp<SkSpecialImage> SkMatrixConvolutionImageFilter::onFilterImage(const Context& ctx,
333 SkIPoint* offset) const {
334 SkIPoint inputOffset = SkIPoint::Make(0, 0);
335 sk_sp<SkSpecialImage> input(this->filterInput(0, ctx, &inputOffset));
336 if (!input) {
337 return nullptr;
338 }
339
340 SkIRect dstBounds;
341 input = this->applyCropRectAndPad(this->mapContext(ctx), input.get(), &inputOffset, &dstBounds);
342 if (!input) {
343 return nullptr;
344 }
345
346 const SkIRect originalSrcBounds = SkIRect::MakeXYWH(inputOffset.fX, inputOffset.fY,
347 input->width(), input->height());
348
349 SkIRect srcBounds = this->onFilterNodeBounds(dstBounds, ctx.ctm(), kReverse_MapDirection,
350 &originalSrcBounds);
351
352 if (SkTileMode::kRepeat == fTileMode || SkTileMode::kMirror == fTileMode) {
353 srcBounds = DetermineRepeatedSrcBound(srcBounds, fKernelOffset,
354 fKernelSize, originalSrcBounds);
355 } else {
356 if (!srcBounds.intersect(dstBounds)) {
357 return nullptr;
358 }
359 }
360
361 #if SK_SUPPORT_GPU
362 if (ctx.gpuBacked()) {
363 auto context = ctx.getContext();
364
365 // Ensure the input is in the destination color space. Typically applyCropRect will have
366 // called pad_image to account for our dilation of bounds, so the result will already be
367 // moved to the destination color space. If a filter DAG avoids that, then we use this
368 // fall-back, which saves us from having to do the xform during the filter itself.
369 input = ImageToColorSpace(input.get(), ctx.colorType(), ctx.colorSpace(),
370 ctx.surfaceProps());
371
372 GrSurfaceProxyView inputView = input->view(context);
373 SkASSERT(inputView.asTextureProxy());
374
375 const auto isProtected = inputView.proxy()->isProtected();
376
377 offset->fX = dstBounds.left();
378 offset->fY = dstBounds.top();
379 dstBounds.offset(-inputOffset);
380 srcBounds.offset(-inputOffset);
381 // Map srcBounds from input's logical image domain to that of the proxy
382 srcBounds.offset(input->subset().x(), input->subset().y());
383
384 auto fp = GrMatrixConvolutionEffect::Make(context,
385 std::move(inputView),
386 srcBounds,
387 fKernelSize,
388 fKernel,
389 fGain,
390 fBias,
391 fKernelOffset,
392 SkTileModeToWrapMode(fTileMode),
393 fConvolveAlpha,
394 *ctx.getContext()->priv().caps());
395 if (!fp) {
396 return nullptr;
397 }
398
399 // FIXME (michaelludwig) - Clean this up as part of the imagefilter refactor, some filters
400 // instead require a coord transform on the FP. At very least, be consistent, at best make
401 // it so that filter impls don't need to worry about the subset origin.
402
403 // Must also map the dstBounds since it is used as the src rect in DrawWithFP when
404 // evaluating the FP, and the dst rect just uses the size of dstBounds.
405 dstBounds.offset(input->subset().x(), input->subset().y());
406 return DrawWithFP(context, std::move(fp), dstBounds, ctx.colorType(), ctx.colorSpace(),
407 ctx.surfaceProps(), isProtected);
408 }
409 #endif
410
411 SkBitmap inputBM;
412 if (!input->getROPixels(&inputBM)) {
413 return nullptr;
414 }
415
416 if (inputBM.colorType() != kN32_SkColorType) {
417 return nullptr;
418 }
419
420 if (!fConvolveAlpha && !inputBM.isOpaque()) {
421 // This leaves the bitmap tagged as premul, which seems weird to me,
422 // but is consistent with old behavior.
423 inputBM.readPixels(inputBM.info().makeAlphaType(kUnpremul_SkAlphaType),
424 inputBM.getPixels(), inputBM.rowBytes(), 0,0);
425 }
426
427 if (!inputBM.getPixels()) {
428 return nullptr;
429 }
430
431 const SkImageInfo info = SkImageInfo::MakeN32(dstBounds.width(), dstBounds.height(),
432 inputBM.alphaType());
433
434 SkBitmap dst;
435 if (!dst.tryAllocPixels(info)) {
436 return nullptr;
437 }
438
439 offset->fX = dstBounds.fLeft;
440 offset->fY = dstBounds.fTop;
441 dstBounds.offset(-inputOffset);
442 srcBounds.offset(-inputOffset);
443
444 SkIRect interior;
445 if (SkTileMode::kRepeat == fTileMode || SkTileMode::kMirror == fTileMode) {
446 // In repeat mode, the filterPixels calls will wrap around
447 // so we just need to render 'dstBounds'
448 interior = dstBounds;
449 } else {
450 interior = SkIRect::MakeXYWH(dstBounds.left() + fKernelOffset.fX,
451 dstBounds.top() + fKernelOffset.fY,
452 dstBounds.width() - fKernelSize.fWidth + 1,
453 dstBounds.height() - fKernelSize.fHeight + 1);
454 }
455
456 SkIRect top = SkIRect::MakeLTRB(dstBounds.left(), dstBounds.top(),
457 dstBounds.right(), interior.top());
458 SkIRect bottom = SkIRect::MakeLTRB(dstBounds.left(), interior.bottom(),
459 dstBounds.right(), dstBounds.bottom());
460 SkIRect left = SkIRect::MakeLTRB(dstBounds.left(), interior.top(),
461 interior.left(), interior.bottom());
462 SkIRect right = SkIRect::MakeLTRB(interior.right(), interior.top(),
463 dstBounds.right(), interior.bottom());
464
465 SkIVector dstContentOffset = { offset->fX - inputOffset.fX, offset->fY - inputOffset.fY };
466
467 this->filterBorderPixels(inputBM, &dst, dstContentOffset, top, srcBounds);
468 this->filterBorderPixels(inputBM, &dst, dstContentOffset, left, srcBounds);
469 this->filterInteriorPixels(inputBM, &dst, dstContentOffset, interior, srcBounds);
470 this->filterBorderPixels(inputBM, &dst, dstContentOffset, right, srcBounds);
471 this->filterBorderPixels(inputBM, &dst, dstContentOffset, bottom, srcBounds);
472
473 return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(dstBounds.width(), dstBounds.height()),
474 dst, ctx.surfaceProps());
475 }
476
onFilterNodeBounds(const SkIRect & src,const SkMatrix & ctm,MapDirection dir,const SkIRect * inputRect) const477 SkIRect SkMatrixConvolutionImageFilter::onFilterNodeBounds(
478 const SkIRect& src, const SkMatrix& ctm, MapDirection dir, const SkIRect* inputRect) const {
479 if (kReverse_MapDirection == dir && inputRect &&
480 (SkTileMode::kRepeat == fTileMode || SkTileMode::kMirror == fTileMode)) {
481 SkASSERT(inputRect);
482 return DetermineRepeatedSrcBound(src, fKernelOffset, fKernelSize, *inputRect);
483 }
484
485 SkIRect dst = src;
486 int w = fKernelSize.width() - 1, h = fKernelSize.height() - 1;
487
488 if (kReverse_MapDirection == dir) {
489 dst.adjust(-fKernelOffset.fX, -fKernelOffset.fY,
490 w - fKernelOffset.fX, h - fKernelOffset.fY);
491 } else {
492 dst.adjust(fKernelOffset.fX - w, fKernelOffset.fY - h, fKernelOffset.fX, fKernelOffset.fY);
493 }
494 return dst;
495 }
496
affectsTransparentBlack() const497 bool SkMatrixConvolutionImageFilter::affectsTransparentBlack() const {
498 // It seems that the only rational way for repeat sample mode to work is if the caller
499 // explicitly restricts the input in which case the input range is explicitly known and
500 // specified.
501 // TODO: is seems that this should be true for clamp mode too.
502
503 // For the other modes, because the kernel is applied in device-space, we have no idea what
504 // pixels it will affect in object-space.
505 return SkTileMode::kRepeat != fTileMode && SkTileMode::kMirror != fTileMode;
506 }
507