• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #if SK_SUPPORT_GPU
21 #include "src/gpu/GrRecordingContextPriv.h"
22 #include "src/gpu/GrTextureProxy.h"
23 #include "src/gpu/SkGr.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 onAffectsTransparentBlack() 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                       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,SkIRect rect,const SkIRect & bounds) const235 void SkMatrixConvolutionImageFilter::filterPixels(const SkBitmap& src,
236                                                   SkBitmap* result,
237                                                   SkIVector& offset,
238                                                   SkIRect rect,
239                                                   const SkIRect& bounds) const {
240     if (!rect.intersect(bounds)) {
241         return;
242     }
243     for (int y = rect.fTop; y < rect.fBottom; ++y) {
244         SkPMColor* dptr = result->getAddr32(rect.fLeft - offset.fX, y - offset.fY);
245         for (int x = rect.fLeft; x < rect.fRight; ++x) {
246             SkScalar sumA = 0, sumR = 0, sumG = 0, sumB = 0;
247             for (int cy = 0; cy < fKernelSize.fHeight; cy++) {
248                 for (int cx = 0; cx < fKernelSize.fWidth; cx++) {
249                     SkPMColor s = PixelFetcher::fetch(src,
250                                                       x + cx - fKernelOffset.fX,
251                                                       y + cy - fKernelOffset.fY,
252                                                       bounds);
253                     SkScalar k = fKernel[cy * fKernelSize.fWidth + cx];
254                     if (convolveAlpha) {
255                         sumA += SkGetPackedA32(s) * k;
256                     }
257                     sumR += SkGetPackedR32(s) * k;
258                     sumG += SkGetPackedG32(s) * k;
259                     sumB += SkGetPackedB32(s) * k;
260                 }
261             }
262             int a = convolveAlpha
263                   ? SkTPin(SkScalarFloorToInt(sumA * fGain + fBias), 0, 255)
264                   : 255;
265             int r = SkTPin(SkScalarFloorToInt(sumR * fGain + fBias), 0, a);
266             int g = SkTPin(SkScalarFloorToInt(sumG * fGain + fBias), 0, a);
267             int b = SkTPin(SkScalarFloorToInt(sumB * fGain + fBias), 0, a);
268             if (!convolveAlpha) {
269                 a = SkGetPackedA32(PixelFetcher::fetch(src, x, y, bounds));
270                 *dptr++ = SkPreMultiplyARGB(a, r, g, b);
271             } else {
272                 *dptr++ = SkPackARGB32(a, r, g, b);
273             }
274         }
275     }
276 }
277 
278 template<class PixelFetcher>
filterPixels(const SkBitmap & src,SkBitmap * result,SkIVector & offset,const SkIRect & rect,const SkIRect & bounds) const279 void SkMatrixConvolutionImageFilter::filterPixels(const SkBitmap& src,
280                                                   SkBitmap* result,
281                                                   SkIVector& offset,
282                                                   const SkIRect& rect,
283                                                   const SkIRect& bounds) const {
284     if (fConvolveAlpha) {
285         filterPixels<PixelFetcher, true>(src, result, offset, rect, bounds);
286     } else {
287         filterPixels<PixelFetcher, false>(src, result, offset, rect, bounds);
288     }
289 }
290 
filterInteriorPixels(const SkBitmap & src,SkBitmap * result,SkIVector & offset,const SkIRect & rect,const SkIRect & bounds) const291 void SkMatrixConvolutionImageFilter::filterInteriorPixels(const SkBitmap& src,
292                                                           SkBitmap* result,
293                                                           SkIVector& offset,
294                                                           const SkIRect& rect,
295                                                           const SkIRect& bounds) const {
296     switch (fTileMode) {
297         case SkTileMode::kMirror:
298             // TODO (michaelludwig) - Implement mirror tiling, treat as repeat for now.
299         case SkTileMode::kRepeat:
300             // In repeat mode, we still need to wrap the samples around the src
301             filterPixels<RepeatPixelFetcher>(src, result, offset, rect, bounds);
302             break;
303         case SkTileMode::kClamp:
304             // Fall through
305         case SkTileMode::kDecal:
306             filterPixels<UncheckedPixelFetcher>(src, result, offset, rect, bounds);
307             break;
308     }
309 }
310 
filterBorderPixels(const SkBitmap & src,SkBitmap * result,SkIVector & offset,const SkIRect & rect,const SkIRect & srcBounds) const311 void SkMatrixConvolutionImageFilter::filterBorderPixels(const SkBitmap& src,
312                                                         SkBitmap* result,
313                                                         SkIVector& offset,
314                                                         const SkIRect& rect,
315                                                         const SkIRect& srcBounds) const {
316     switch (fTileMode) {
317         case SkTileMode::kClamp:
318             filterPixels<ClampPixelFetcher>(src, result, offset, rect, srcBounds);
319             break;
320         case SkTileMode::kMirror:
321             // TODO (michaelludwig) - Implement mirror tiling, treat as repeat for now.
322         case SkTileMode::kRepeat:
323             filterPixels<RepeatPixelFetcher>(src, result, offset, rect, srcBounds);
324             break;
325         case SkTileMode::kDecal:
326             filterPixels<ClampToBlackPixelFetcher>(src, result, offset, rect, srcBounds);
327             break;
328     }
329 }
330 
onFilterImage(const Context & ctx,SkIPoint * offset) const331 sk_sp<SkSpecialImage> SkMatrixConvolutionImageFilter::onFilterImage(const Context& ctx,
332                                                                     SkIPoint* offset) const {
333     SkIPoint inputOffset = SkIPoint::Make(0, 0);
334     sk_sp<SkSpecialImage> input(this->filterInput(0, ctx, &inputOffset));
335     if (!input) {
336         return nullptr;
337     }
338 
339     SkIRect dstBounds;
340     input = this->applyCropRectAndPad(this->mapContext(ctx), input.get(), &inputOffset, &dstBounds);
341     if (!input) {
342         return nullptr;
343     }
344 
345     const SkIRect originalSrcBounds = SkIRect::MakeXYWH(inputOffset.fX, inputOffset.fY,
346                                                         input->width(), input->height());
347 
348     SkIRect srcBounds = this->onFilterNodeBounds(dstBounds, ctx.ctm(), kReverse_MapDirection,
349                                                  &originalSrcBounds);
350 
351     if (SkTileMode::kRepeat == fTileMode || SkTileMode::kMirror == fTileMode) {
352         srcBounds = DetermineRepeatedSrcBound(srcBounds, fKernelOffset,
353                                               fKernelSize, originalSrcBounds);
354     } else {
355         if (!srcBounds.intersect(dstBounds)) {
356             return nullptr;
357         }
358     }
359 
360 #if SK_SUPPORT_GPU
361     if (ctx.gpuBacked()) {
362         auto context = ctx.getContext();
363 
364         // Ensure the input is in the destination color space. Typically applyCropRect will have
365         // called pad_image to account for our dilation of bounds, so the result will already be
366         // moved to the destination color space. If a filter DAG avoids that, then we use this
367         // fall-back, which saves us from having to do the xform during the filter itself.
368         input = ImageToColorSpace(input.get(), ctx.colorType(), ctx.colorSpace(),
369                                   ctx.surfaceProps());
370 
371         GrSurfaceProxyView inputView = input->view(context);
372         SkASSERT(inputView.asTextureProxy());
373 
374         const auto isProtected = inputView.proxy()->isProtected();
375 
376         offset->fX = dstBounds.left();
377         offset->fY = dstBounds.top();
378         dstBounds.offset(-inputOffset);
379         srcBounds.offset(-inputOffset);
380         // Map srcBounds from input's logical image domain to that of the proxy
381         srcBounds.offset(input->subset().x(), input->subset().y());
382 
383         auto fp = GrMatrixConvolutionEffect::Make(context,
384                                                   std::move(inputView),
385                                                   srcBounds,
386                                                   fKernelSize,
387                                                   fKernel,
388                                                   fGain,
389                                                   fBias,
390                                                   fKernelOffset,
391                                                   SkTileModeToWrapMode(fTileMode),
392                                                   fConvolveAlpha,
393                                                   *ctx.getContext()->priv().caps());
394         if (!fp) {
395             return nullptr;
396         }
397 
398         // FIXME (michaelludwig) - Clean this up as part of the imagefilter refactor, some filters
399         // instead require a coord transform on the FP. At very least, be consistent, at best make
400         // it so that filter impls don't need to worry about the subset origin.
401 
402         // Must also map the dstBounds since it is used as the src rect in DrawWithFP when
403         // evaluating the FP, and the dst rect just uses the size of dstBounds.
404         dstBounds.offset(input->subset().x(), input->subset().y());
405         return DrawWithFP(context, std::move(fp), dstBounds, ctx.colorType(), ctx.colorSpace(),
406                           ctx.surfaceProps(), isProtected);
407     }
408 #endif
409 
410     SkBitmap inputBM;
411     if (!input->getROPixels(&inputBM)) {
412         return nullptr;
413     }
414 
415     if (inputBM.colorType() != kN32_SkColorType) {
416         return nullptr;
417     }
418 
419     if (!fConvolveAlpha && !inputBM.isOpaque()) {
420         // This leaves the bitmap tagged as premul, which seems weird to me,
421         // but is consistent with old behavior.
422         inputBM.readPixels(inputBM.info().makeAlphaType(kUnpremul_SkAlphaType),
423                            inputBM.getPixels(), inputBM.rowBytes(), 0,0);
424     }
425 
426     if (!inputBM.getPixels()) {
427         return nullptr;
428     }
429 
430     const SkImageInfo info = SkImageInfo::MakeN32(dstBounds.width(), dstBounds.height(),
431                                                   inputBM.alphaType());
432 
433     SkBitmap dst;
434     if (!dst.tryAllocPixels(info)) {
435         return nullptr;
436     }
437 
438     offset->fX = dstBounds.fLeft;
439     offset->fY = dstBounds.fTop;
440     dstBounds.offset(-inputOffset);
441     srcBounds.offset(-inputOffset);
442 
443     SkIRect interior;
444     if (SkTileMode::kRepeat == fTileMode || SkTileMode::kMirror == fTileMode) {
445         // In repeat mode, the filterPixels calls will wrap around
446         // so we just need to render 'dstBounds'
447         interior = dstBounds;
448     } else {
449         interior = SkIRect::MakeXYWH(dstBounds.left() + fKernelOffset.fX,
450                                      dstBounds.top() + fKernelOffset.fY,
451                                      dstBounds.width() - fKernelSize.fWidth + 1,
452                                      dstBounds.height() - fKernelSize.fHeight + 1);
453     }
454 
455     SkIRect top = SkIRect::MakeLTRB(dstBounds.left(), dstBounds.top(),
456                                     dstBounds.right(), interior.top());
457     SkIRect bottom = SkIRect::MakeLTRB(dstBounds.left(), interior.bottom(),
458                                        dstBounds.right(), dstBounds.bottom());
459     SkIRect left = SkIRect::MakeLTRB(dstBounds.left(), interior.top(),
460                                      interior.left(), interior.bottom());
461     SkIRect right = SkIRect::MakeLTRB(interior.right(), interior.top(),
462                                       dstBounds.right(), interior.bottom());
463 
464     SkIVector dstContentOffset = { offset->fX - inputOffset.fX, offset->fY - inputOffset.fY };
465 
466     this->filterBorderPixels(inputBM, &dst, dstContentOffset, top, srcBounds);
467     this->filterBorderPixels(inputBM, &dst, dstContentOffset, left, srcBounds);
468     this->filterInteriorPixels(inputBM, &dst, dstContentOffset, interior, srcBounds);
469     this->filterBorderPixels(inputBM, &dst, dstContentOffset, right, srcBounds);
470     this->filterBorderPixels(inputBM, &dst, dstContentOffset, bottom, srcBounds);
471 
472     return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(dstBounds.width(), dstBounds.height()),
473                                           dst, ctx.surfaceProps());
474 }
475 
onFilterNodeBounds(const SkIRect & src,const SkMatrix & ctm,MapDirection dir,const SkIRect * inputRect) const476 SkIRect SkMatrixConvolutionImageFilter::onFilterNodeBounds(
477         const SkIRect& src, const SkMatrix& ctm, MapDirection dir, const SkIRect* inputRect) const {
478     if (kReverse_MapDirection == dir && inputRect &&
479         (SkTileMode::kRepeat == fTileMode || SkTileMode::kMirror == fTileMode)) {
480         SkASSERT(inputRect);
481         return DetermineRepeatedSrcBound(src, fKernelOffset, fKernelSize, *inputRect);
482     }
483 
484     SkIRect dst = src;
485     int w = fKernelSize.width() - 1, h = fKernelSize.height() - 1;
486 
487     if (kReverse_MapDirection == dir) {
488         dst.adjust(-fKernelOffset.fX, -fKernelOffset.fY,
489                    w - fKernelOffset.fX, h - fKernelOffset.fY);
490     } else {
491         dst.adjust(fKernelOffset.fX - w, fKernelOffset.fY - h, fKernelOffset.fX, fKernelOffset.fY);
492     }
493     return dst;
494 }
495 
onAffectsTransparentBlack() const496 bool SkMatrixConvolutionImageFilter::onAffectsTransparentBlack() const {
497     // It seems that the only rational way for repeat sample mode to work is if the caller
498     // explicitly restricts the input in which case the input range is explicitly known and
499     // specified.
500     // TODO: is seems that this should be true for clamp mode too.
501 
502     // For the other modes, because the kernel is applied in device-space, we have no idea what
503     // pixels it will affect in object-space.
504     return SkTileMode::kRepeat != fTileMode && SkTileMode::kMirror != fTileMode;
505 }
506