1 /*
2 * Copyright 2011 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/effects/SkBlurImageFilter.h"
9
10 #include <algorithm>
11
12 #include "include/core/SkBitmap.h"
13 #include "include/core/SkTileMode.h"
14 #include "include/private/SkColorData.h"
15 #include "include/private/SkNx.h"
16 #include "include/private/SkTFitsIn.h"
17 #include "src/core/SkArenaAlloc.h"
18 #include "src/core/SkAutoPixmapStorage.h"
19 #include "src/core/SkGpuBlurUtils.h"
20 #include "src/core/SkImageFilter_Base.h"
21 #include "src/core/SkOpts.h"
22 #include "src/core/SkReadBuffer.h"
23 #include "src/core/SkSpecialImage.h"
24 #include "src/core/SkWriteBuffer.h"
25
26 #if SK_SUPPORT_GPU
27 #include "include/gpu/GrContext.h"
28 #include "src/gpu/GrTextureProxy.h"
29 #include "src/gpu/SkGr.h"
30 #endif
31
32 namespace {
33
34 class SkBlurImageFilterImpl final : public SkImageFilter_Base {
35 public:
SkBlurImageFilterImpl(SkScalar sigmaX,SkScalar sigmaY,SkTileMode tileMode,sk_sp<SkImageFilter> input,const CropRect * cropRect)36 SkBlurImageFilterImpl(SkScalar sigmaX, SkScalar sigmaY, SkTileMode tileMode,
37 sk_sp<SkImageFilter> input, const CropRect* cropRect)
38 : INHERITED(&input, 1, cropRect)
39 , fSigma{sigmaX, sigmaY}
40 , fTileMode(tileMode) {}
41
42 SkRect computeFastBounds(const SkRect&) const override;
43
44 protected:
45 void flatten(SkWriteBuffer&) const override;
46 sk_sp<SkSpecialImage> onFilterImage(const Context&, SkIPoint* offset) const override;
47 SkIRect onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm,
48 MapDirection, const SkIRect* inputRect) const override;
49
50 private:
51 friend void SkBlurImageFilter::RegisterFlattenables();
52 SK_FLATTENABLE_HOOKS(SkBlurImageFilterImpl)
53
54 #if SK_SUPPORT_GPU
55 sk_sp<SkSpecialImage> gpuFilter(
56 const Context& ctx, SkVector sigma,
57 const sk_sp<SkSpecialImage> &input,
58 SkIRect inputBounds, SkIRect dstBounds, SkIPoint inputOffset, SkIPoint* offset) const;
59 #endif
60
61 SkSize fSigma;
62 SkTileMode fTileMode;
63
64 typedef SkImageFilter_Base INHERITED;
65 };
66
67 } // end namespace
68
to_sktilemode(SkBlurImageFilter::TileMode tileMode)69 static SkTileMode to_sktilemode(SkBlurImageFilter::TileMode tileMode) {
70 switch(tileMode) {
71 case SkBlurImageFilter::kClamp_TileMode:
72 return SkTileMode::kClamp;
73 case SkBlurImageFilter::kRepeat_TileMode:
74 return SkTileMode::kRepeat;
75 case SkBlurImageFilter::kClampToBlack_TileMode:
76 // Fall through
77 default:
78 return SkTileMode::kDecal;
79 }
80 }
81
Make(SkScalar sigmaX,SkScalar sigmaY,sk_sp<SkImageFilter> input,const SkImageFilter::CropRect * cropRect,TileMode tileMode)82 sk_sp<SkImageFilter> SkBlurImageFilter::Make(SkScalar sigmaX, SkScalar sigmaY,
83 sk_sp<SkImageFilter> input,
84 const SkImageFilter::CropRect* cropRect,
85 TileMode tileMode) {
86 return Make(sigmaX, sigmaY, to_sktilemode(tileMode), std::move(input), cropRect);
87 }
88
Make(SkScalar sigmaX,SkScalar sigmaY,SkTileMode tileMode,sk_sp<SkImageFilter> input,const SkImageFilter::CropRect * cropRect)89 sk_sp<SkImageFilter> SkBlurImageFilter::Make(SkScalar sigmaX, SkScalar sigmaY, SkTileMode tileMode,
90 sk_sp<SkImageFilter> input,
91 const SkImageFilter::CropRect* cropRect) {
92 if (sigmaX < SK_ScalarNearlyZero && sigmaY < SK_ScalarNearlyZero && !cropRect) {
93 return input;
94 }
95 return sk_sp<SkImageFilter>(
96 new SkBlurImageFilterImpl(sigmaX, sigmaY, tileMode, input, cropRect));
97 }
98
RegisterFlattenables()99 void SkBlurImageFilter::RegisterFlattenables() { SK_REGISTER_FLATTENABLE(SkBlurImageFilterImpl); }
100
101 ///////////////////////////////////////////////////////////////////////////////
102
CreateProc(SkReadBuffer & buffer)103 sk_sp<SkFlattenable> SkBlurImageFilterImpl::CreateProc(SkReadBuffer& buffer) {
104 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
105 SkScalar sigmaX = buffer.readScalar();
106 SkScalar sigmaY = buffer.readScalar();
107 SkTileMode tileMode;
108 if (buffer.isVersionLT(SkPicturePriv::kTileModeInBlurImageFilter_Version)) {
109 tileMode = SkTileMode::kDecal;
110 } else if (buffer.isVersionLT(SkPicturePriv::kCleanupImageFilterEnums_Version)) {
111 tileMode = to_sktilemode(buffer.read32LE(SkBlurImageFilter::kLast_TileMode));
112 } else {
113 tileMode = buffer.read32LE(SkTileMode::kLastTileMode);
114 }
115
116 static_assert(SkBlurImageFilter::kLast_TileMode == 2, "CreateProc");
117
118 return SkBlurImageFilter::Make(
119 sigmaX, sigmaY, tileMode, common.getInput(0), &common.cropRect());
120 }
121
flatten(SkWriteBuffer & buffer) const122 void SkBlurImageFilterImpl::flatten(SkWriteBuffer& buffer) const {
123 this->INHERITED::flatten(buffer);
124 buffer.writeScalar(fSigma.fWidth);
125 buffer.writeScalar(fSigma.fHeight);
126
127 // Fuzzer sanity checks
128 static_assert((int) SkTileMode::kLastTileMode == 3 && SkBlurImageFilter::kLast_TileMode == 2,
129 "SkBlurImageFilterImpl::flatten");
130 SkASSERT(fTileMode <= SkTileMode::kLastTileMode);
131 buffer.writeInt(static_cast<int>(fTileMode));
132 }
133
134 #if SK_SUPPORT_GPU
to_texture_domain_mode(SkTileMode tileMode)135 static GrTextureDomain::Mode to_texture_domain_mode(SkTileMode tileMode) {
136 switch (tileMode) {
137 case SkTileMode::kClamp:
138 return GrTextureDomain::kClamp_Mode;
139 case SkTileMode::kDecal:
140 return GrTextureDomain::kDecal_Mode;
141 case SkTileMode::kMirror:
142 // TODO (michaelludwig) - Support mirror mode, treat as repeat for now
143 case SkTileMode::kRepeat:
144 return GrTextureDomain::kRepeat_Mode;
145 default:
146 SK_ABORT("Unsupported tile mode.");
147 }
148 }
149 #endif
150
151 // This is defined by the SVG spec:
152 // https://drafts.fxtf.org/filter-effects/#feGaussianBlurElement
calculate_window(double sigma)153 static int calculate_window(double sigma) {
154 // NB 136 is the largest sigma that will not cause a buffer full of 255 mask values to overflow
155 // using the Gauss filter. It also limits the size of buffers used hold intermediate values.
156 // Explanation of maximums:
157 // sum0 = window * 255
158 // sum1 = window * sum0 -> window * window * 255
159 // sum2 = window * sum1 -> window * window * window * 255 -> window^3 * 255
160 //
161 // The value window^3 * 255 must fit in a uint32_t. So,
162 // window^3 < 2^32. window = 255.
163 //
164 // window = floor(sigma * 3 * sqrt(2 * kPi) / 4 + 0.5)
165 // For window <= 255, the largest value for sigma is 136.
166 sigma = SkTPin(sigma, 0.0, 136.0);
167 auto possibleWindow = static_cast<int>(floor(sigma * 3 * sqrt(2 * SK_DoublePI) / 4 + 0.5));
168 return std::max(1, possibleWindow);
169 }
170
171 // Calculating the border is tricky. The border is the distance in pixels between the first dst
172 // pixel and the first src pixel (or the last src pixel and the last dst pixel).
173 // I will go through the odd case which is simpler, and then through the even case. Given a
174 // stack of filters seven wide for the odd case of three passes.
175 //
176 // S
177 // aaaAaaa
178 // bbbBbbb
179 // cccCccc
180 // D
181 //
182 // The furthest changed pixel is when the filters are in the following configuration.
183 //
184 // S
185 // aaaAaaa
186 // bbbBbbb
187 // cccCccc
188 // D
189 //
190 // The A pixel is calculated using the value S, the B uses A, and the C uses B, and
191 // finally D is C. So, with a window size of seven the border is nine. In the odd case, the
192 // border is 3*((window - 1)/2).
193 //
194 // For even cases the filter stack is more complicated. The spec specifies two passes
195 // of even filters and a final pass of odd filters. A stack for a width of six looks like
196 // this.
197 //
198 // S
199 // aaaAaa
200 // bbBbbb
201 // cccCccc
202 // D
203 //
204 // The furthest pixel looks like this.
205 //
206 // S
207 // aaaAaa
208 // bbBbbb
209 // cccCccc
210 // D
211 //
212 // For a window of six, the border value is eight. In the even case the border is 3 *
213 // (window/2) - 1.
calculate_border(int window)214 static int calculate_border(int window) {
215 return (window & 1) == 1 ? 3 * ((window - 1) / 2) : 3 * (window / 2) - 1;
216 }
217
calculate_buffer(int window)218 static int calculate_buffer(int window) {
219 int bufferSize = window - 1;
220 return (window & 1) == 1 ? 3 * bufferSize : 3 * bufferSize + 1;
221 }
222
223 // blur_one_direction implements the common three pass box filter approximation of Gaussian blur,
224 // but combines all three passes into a single pass. This approach is facilitated by three circular
225 // buffers the width of the window which track values for trailing edges of each of the three
226 // passes. This allows the algorithm to use more precision in the calculation because the values
227 // are not rounded each pass. And this implementation also avoids a trap that's easy to fall
228 // into resulting in blending in too many zeroes near the edge.
229 //
230 // In general, a window sum has the form:
231 // sum_n+1 = sum_n + leading_edge - trailing_edge.
232 // If instead we do the subtraction at the end of the previous iteration, we can just
233 // calculate the sums instead of having to do the subtractions too.
234 //
235 // In previous iteration:
236 // sum_n+1 = sum_n - trailing_edge.
237 //
238 // In this iteration:
239 // sum_n+1 = sum_n + leading_edge.
240 //
241 // Now we can stack all three sums and do them at once. Sum0 gets its leading edge from the
242 // actual data. Sum1's leading edge is just Sum0, and Sum2's leading edge is Sum1. So, doing the
243 // three passes at the same time has the form:
244 //
245 // sum0_n+1 = sum0_n + leading edge
246 // sum1_n+1 = sum1_n + sum0_n+1
247 // sum2_n+1 = sum2_n + sum1_n+1
248 //
249 // sum2_n+1 / window^3 is the new value of the destination pixel.
250 //
251 // Reduce the sums by the trailing edges which were stored in the circular buffers,
252 // for the next go around. This is the case for odd sized windows, even windows the the third
253 // circular buffer is one larger then the first two circular buffers.
254 //
255 // sum2_n+2 = sum2_n+1 - buffer2[i];
256 // buffer2[i] = sum1;
257 // sum1_n+2 = sum1_n+1 - buffer1[i];
258 // buffer1[i] = sum0;
259 // sum0_n+2 = sum0_n+1 - buffer0[i];
260 // buffer0[i] = leading edge
261 //
262 // This is all encapsulated in the processValue function below.
263 //
264 using Pass0And1 = Sk4u[2];
265 // The would be dLeft parameter is assumed to be 0.
blur_one_direction(Sk4u * buffer,int window,int srcLeft,int srcRight,int dstRight,const uint32_t * src,int srcXStride,int srcYStride,int srcH,uint32_t * dst,int dstXStride,int dstYStride)266 static void blur_one_direction(Sk4u* buffer, int window,
267 int srcLeft, int srcRight, int dstRight,
268 const uint32_t* src, int srcXStride, int srcYStride, int srcH,
269 uint32_t* dst, int dstXStride, int dstYStride) {
270
271 // The circular buffers are one less than the window.
272 auto pass0Count = window - 1,
273 pass1Count = window - 1,
274 pass2Count = (window & 1) == 1 ? window - 1 : window;
275
276 Pass0And1* buffer01Start = (Pass0And1*)buffer;
277 Sk4u* buffer2Start = buffer + pass0Count + pass1Count;
278 Pass0And1* buffer01End = (Pass0And1*)buffer2Start;
279 Sk4u* buffer2End = buffer2Start + pass2Count;
280
281 // If the window is odd then the divisor is just window ^ 3 otherwise,
282 // it is window * window * (window + 1) = window ^ 3 + window ^ 2;
283 auto window2 = window * window;
284 auto window3 = window2 * window;
285 auto divisor = (window & 1) == 1 ? window3 : window3 + window2;
286
287 // NB the sums in the blur code use the following technique to avoid
288 // adding 1/2 to round the divide.
289 //
290 // Sum/d + 1/2 == (Sum + h) / d
291 // Sum + d(1/2) == Sum + h
292 // h == (1/2)d
293 //
294 // But the d/2 it self should be rounded.
295 // h == d/2 + 1/2 == (d + 1) / 2
296 //
297 // weight = 1 / d * 2 ^ 32
298 auto weight = static_cast<uint32_t>(round(1.0 / divisor * (1ull << 32)));
299 auto half = static_cast<uint32_t>((divisor + 1) / 2);
300
301 auto border = calculate_border(window);
302
303 // Calculate the start and end of the source pixels with respect to the destination start.
304 auto srcStart = srcLeft - border,
305 srcEnd = srcRight - border,
306 dstEnd = dstRight;
307
308 for (auto y = 0; y < srcH; y++) {
309 auto buffer01Cursor = buffer01Start;
310 auto buffer2Cursor = buffer2Start;
311
312 Sk4u sum0{0u};
313 Sk4u sum1{0u};
314 Sk4u sum2{half};
315
316 sk_bzero(buffer01Start, (buffer2End - (Sk4u *) (buffer01Start)) * sizeof(*buffer2Start));
317
318 // Given an expanded input pixel, move the window ahead using the leadingEdge value.
319 auto processValue = [&](const Sk4u& leadingEdge) -> Sk4u {
320 sum0 += leadingEdge;
321 sum1 += sum0;
322 sum2 += sum1;
323
324 Sk4u value = sum2.mulHi(weight);
325
326 sum2 -= *buffer2Cursor;
327 *buffer2Cursor = sum1;
328 buffer2Cursor = (buffer2Cursor + 1) < buffer2End ? buffer2Cursor + 1 : buffer2Start;
329
330 sum1 -= (*buffer01Cursor)[1];
331 (*buffer01Cursor)[1] = sum0;
332 sum0 -= (*buffer01Cursor)[0];
333 (*buffer01Cursor)[0] = leadingEdge;
334 buffer01Cursor =
335 (buffer01Cursor + 1) < buffer01End ? buffer01Cursor + 1 : buffer01Start;
336
337 return value;
338 };
339
340 auto srcIdx = srcStart;
341 auto dstIdx = 0;
342 const uint32_t* srcCursor = src;
343 uint32_t* dstCursor = dst;
344
345 // The destination pixels are not effected by the src pixels,
346 // change to zero as per the spec.
347 // https://drafts.fxtf.org/filter-effects/#FilterPrimitivesOverviewIntro
348 while (dstIdx < srcIdx) {
349 *dstCursor = 0;
350 dstCursor += dstXStride;
351 SK_PREFETCH(dstCursor);
352 dstIdx++;
353 }
354
355 // The edge of the source is before the edge of the destination. Calculate the sums for
356 // the pixels before the start of the destination.
357 while (dstIdx > srcIdx) {
358 Sk4u leadingEdge = srcIdx < srcEnd ? SkNx_cast<uint32_t>(Sk4b::Load(srcCursor)) : 0;
359 (void) processValue(leadingEdge);
360 srcCursor += srcXStride;
361 srcIdx++;
362 }
363
364 // The dstIdx and srcIdx are in sync now; the code just uses the dstIdx for both now.
365 // Consume the source generating pixels to dst.
366 auto loopEnd = std::min(dstEnd, srcEnd);
367 while (dstIdx < loopEnd) {
368 Sk4u leadingEdge = SkNx_cast<uint32_t>(Sk4b::Load(srcCursor));
369 SkNx_cast<uint8_t>(processValue(leadingEdge)).store(dstCursor);
370 srcCursor += srcXStride;
371 dstCursor += dstXStride;
372 SK_PREFETCH(dstCursor);
373 dstIdx++;
374 }
375
376 // The leading edge is beyond the end of the source. Assume that the pixels
377 // are now 0x0000 until the end of the destination.
378 loopEnd = dstEnd;
379 while (dstIdx < loopEnd) {
380 SkNx_cast<uint8_t>(processValue(0u)).store(dstCursor);
381 dstCursor += dstXStride;
382 SK_PREFETCH(dstCursor);
383 dstIdx++;
384 }
385
386 src += srcYStride;
387 dst += dstYStride;
388 }
389 }
390
copy_image_with_bounds(const SkImageFilter_Base::Context & ctx,const sk_sp<SkSpecialImage> & input,SkIRect srcBounds,SkIRect dstBounds)391 static sk_sp<SkSpecialImage> copy_image_with_bounds(
392 const SkImageFilter_Base::Context& ctx, const sk_sp<SkSpecialImage> &input,
393 SkIRect srcBounds, SkIRect dstBounds) {
394 SkBitmap inputBM;
395 if (!input->getROPixels(&inputBM)) {
396 return nullptr;
397 }
398
399 if (inputBM.colorType() != kN32_SkColorType) {
400 return nullptr;
401 }
402
403 SkBitmap src;
404 inputBM.extractSubset(&src, srcBounds);
405
406 // Make everything relative to the destination bounds.
407 srcBounds.offset(-dstBounds.x(), -dstBounds.y());
408 dstBounds.offset(-dstBounds.x(), -dstBounds.y());
409
410 auto srcW = srcBounds.width(),
411 dstW = dstBounds.width(),
412 dstH = dstBounds.height();
413
414 SkImageInfo dstInfo = SkImageInfo::Make(dstW, dstH, inputBM.colorType(), inputBM.alphaType());
415
416 SkBitmap dst;
417 if (!dst.tryAllocPixels(dstInfo)) {
418 return nullptr;
419 }
420
421 // There is no blurring to do, but we still need to copy the source while accounting for the
422 // dstBounds. Remember that the src was intersected with the dst.
423 int y = 0;
424 size_t dstWBytes = dstW * sizeof(uint32_t);
425 for (;y < srcBounds.top(); y++) {
426 sk_bzero(dst.getAddr32(0, y), dstWBytes);
427 }
428
429 for (;y < srcBounds.bottom(); y++) {
430 int x = 0;
431 uint32_t* dstPtr = dst.getAddr32(0, y);
432 for (;x < srcBounds.left(); x++) {
433 *dstPtr++ = 0;
434 }
435
436 memcpy(dstPtr, src.getAddr32(x - srcBounds.left(), y - srcBounds.top()),
437 srcW * sizeof(uint32_t));
438
439 dstPtr += srcW;
440 x += srcW;
441
442 for (;x < dstBounds.right(); x++) {
443 *dstPtr++ = 0;
444 }
445 }
446
447 for (;y < dstBounds.bottom(); y++) {
448 sk_bzero(dst.getAddr32(0, y), dstWBytes);
449 }
450
451 return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(dstBounds.width(),
452 dstBounds.height()),
453 dst, ctx.surfaceProps());
454 }
455
456 // TODO: Implement CPU backend for different fTileMode.
cpu_blur(const SkImageFilter_Base::Context & ctx,SkVector sigma,const sk_sp<SkSpecialImage> & input,SkIRect srcBounds,SkIRect dstBounds)457 static sk_sp<SkSpecialImage> cpu_blur(
458 const SkImageFilter_Base::Context& ctx,
459 SkVector sigma, const sk_sp<SkSpecialImage> &input,
460 SkIRect srcBounds, SkIRect dstBounds) {
461 auto windowW = calculate_window(sigma.x()),
462 windowH = calculate_window(sigma.y());
463
464 if (windowW <= 1 && windowH <= 1) {
465 return copy_image_with_bounds(ctx, input, srcBounds, dstBounds);
466 }
467
468 SkBitmap inputBM;
469
470 if (!input->getROPixels(&inputBM)) {
471 return nullptr;
472 }
473
474 if (inputBM.colorType() != kN32_SkColorType) {
475 return nullptr;
476 }
477
478 SkBitmap src;
479 inputBM.extractSubset(&src, srcBounds);
480
481 // Make everything relative to the destination bounds.
482 srcBounds.offset(-dstBounds.x(), -dstBounds.y());
483 dstBounds.offset(-dstBounds.x(), -dstBounds.y());
484
485 auto srcW = srcBounds.width(),
486 srcH = srcBounds.height(),
487 dstW = dstBounds.width(),
488 dstH = dstBounds.height();
489
490 SkImageInfo dstInfo = inputBM.info().makeWH(dstW, dstH);
491
492 SkBitmap dst;
493 if (!dst.tryAllocPixels(dstInfo)) {
494 return nullptr;
495 }
496
497 auto bufferSizeW = calculate_buffer(windowW),
498 bufferSizeH = calculate_buffer(windowH);
499
500 // The amount 1024 is enough for buffers up to 10 sigma. The tmp bitmap will be
501 // allocated on the heap.
502 SkSTArenaAlloc<1024> alloc;
503 Sk4u* buffer = alloc.makeArrayDefault<Sk4u>(std::max(bufferSizeW, bufferSizeH));
504
505 // Basic Plan: The three cases to handle
506 // * Horizontal and Vertical - blur horizontally while copying values from the source to
507 // the destination. Then, do an in-place vertical blur.
508 // * Horizontal only - blur horizontally copying values from the source to the destination.
509 // * Vertical only - blur vertically copying values from the source to the destination.
510
511 // Default to vertical only blur case. If a horizontal blur is needed, then these values
512 // will be adjusted while doing the horizontal blur.
513 auto intermediateSrc = static_cast<uint32_t *>(src.getPixels());
514 auto intermediateRowBytesAsPixels = src.rowBytesAsPixels();
515 auto intermediateWidth = srcW;
516
517 // Because the border is calculated before the fork of the GPU/CPU path. The border is
518 // the maximum of the two rendering methods. In the case where sigma is zero, then the
519 // src and dst left values are the same. If sigma is small resulting in a window size of
520 // 1, then border calculations add some pixels which will always be zero. Inset the
521 // destination by those zero pixels. This case is very rare.
522 auto intermediateDst = dst.getAddr32(srcBounds.left(), 0);
523
524 // The following code is executed very rarely, I have never seen it in a real web
525 // page. If sigma is small but not zero then shared GPU/CPU border calculation
526 // code adds extra pixels for the border. Just clear everything to clear those pixels.
527 // This solution is overkill, but very simple.
528 if (windowW == 1 || windowH == 1) {
529 dst.eraseColor(0);
530 }
531
532 if (windowW > 1) {
533 // Make int64 to avoid overflow in multiplication below.
534 int64_t shift = srcBounds.top() - dstBounds.top();
535
536 // For the horizontal blur, starts part way down in anticipation of the vertical blur.
537 // For a vertical sigma of zero shift should be zero. But, for small sigma,
538 // shift may be > 0 but the vertical window could be 1.
539 intermediateSrc = static_cast<uint32_t *>(dst.getPixels())
540 + (shift > 0 ? shift * dst.rowBytesAsPixels() : 0);
541 intermediateRowBytesAsPixels = dst.rowBytesAsPixels();
542 intermediateWidth = dstW;
543 intermediateDst = static_cast<uint32_t *>(dst.getPixels());
544
545 blur_one_direction(
546 buffer, windowW,
547 srcBounds.left(), srcBounds.right(), dstBounds.right(),
548 static_cast<uint32_t *>(src.getPixels()), 1, src.rowBytesAsPixels(), srcH,
549 intermediateSrc, 1, intermediateRowBytesAsPixels);
550 }
551
552 if (windowH > 1) {
553 blur_one_direction(
554 buffer, windowH,
555 srcBounds.top(), srcBounds.bottom(), dstBounds.bottom(),
556 intermediateSrc, intermediateRowBytesAsPixels, 1, intermediateWidth,
557 intermediateDst, dst.rowBytesAsPixels(), 1);
558 }
559
560 return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(dstBounds.width(),
561 dstBounds.height()),
562 dst, ctx.surfaceProps());
563 }
564
565 // This rather arbitrary-looking value results in a maximum box blur kernel size
566 // of 1000 pixels on the raster path, which matches the WebKit and Firefox
567 // implementations. Since the GPU path does not compute a box blur, putting
568 // the limit on sigma ensures consistent behaviour between the GPU and
569 // raster paths.
570 #define MAX_SIGMA SkIntToScalar(532)
571
map_sigma(const SkSize & localSigma,const SkMatrix & ctm)572 static SkVector map_sigma(const SkSize& localSigma, const SkMatrix& ctm) {
573 SkVector sigma = SkVector::Make(localSigma.width(), localSigma.height());
574 ctm.mapVectors(&sigma, 1);
575 sigma.fX = SkMinScalar(SkScalarAbs(sigma.fX), MAX_SIGMA);
576 sigma.fY = SkMinScalar(SkScalarAbs(sigma.fY), MAX_SIGMA);
577 return sigma;
578 }
579
onFilterImage(const Context & ctx,SkIPoint * offset) const580 sk_sp<SkSpecialImage> SkBlurImageFilterImpl::onFilterImage(const Context& ctx,
581 SkIPoint* offset) const {
582 SkIPoint inputOffset = SkIPoint::Make(0, 0);
583
584 sk_sp<SkSpecialImage> input(this->filterInput(0, ctx, &inputOffset));
585 if (!input) {
586 return nullptr;
587 }
588
589 SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.fX, inputOffset.fY,
590 input->width(), input->height());
591
592 // Calculate the destination bounds.
593 SkIRect dstBounds;
594 if (!this->applyCropRect(this->mapContext(ctx), inputBounds, &dstBounds)) {
595 return nullptr;
596 }
597 if (!inputBounds.intersect(dstBounds)) {
598 return nullptr;
599 }
600
601 // Save the offset in preparation to make all rectangles relative to the inputOffset.
602 SkIPoint resultOffset = SkIPoint::Make(dstBounds.fLeft, dstBounds.fTop);
603
604 // Make all bounds relative to the inputOffset.
605 inputBounds.offset(-inputOffset);
606 dstBounds.offset(-inputOffset);
607
608 SkVector sigma = map_sigma(fSigma, ctx.ctm());
609 if (sigma.x() < 0 || sigma.y() < 0) {
610 return nullptr;
611 }
612
613 sk_sp<SkSpecialImage> result;
614 #if SK_SUPPORT_GPU
615 if (ctx.gpuBacked()) {
616 // Ensure the input is in the destination's gamut. This saves us from having to do the
617 // xform during the filter itself.
618 input = ImageToColorSpace(input.get(), ctx.colorType(), ctx.colorSpace());
619 result = this->gpuFilter(ctx, sigma, input, inputBounds, dstBounds, inputOffset,
620 &resultOffset);
621 } else
622 #endif
623 {
624 // NB 135 is the largest sigma that will not cause a buffer full of 255 mask values to overflow
625 // using the Gauss filter. It also limits the size of buffers used hold intermediate values. The
626 // additional + 1 added to window represents adding one more leading element before subtracting the
627 // trailing element.
628 // Explanation of maximums:
629 // sum0 = (window + 1) * 255
630 // sum1 = (window + 1) * sum0 -> (window + 1) * (window + 1) * 255
631 // sum2 = (window + 1) * sum1 -> (window + 1) * (window + 1) * (window + 1) * 255 -> window^3 * 255
632 //
633 // The value (window + 1)^3 * 255 must fit in a uint32_t. So,
634 // (window + 1)^3 * 255 < 2^32. window = 255.
635 //
636 // window = floor(sigma * 3 * sqrt(2 * kPi) / 4)
637 // For window <= 255, the largest value for sigma is 135.
638 sigma.fX = SkTPin(sigma.fX, 0.0f, 135.0f);
639 sigma.fY = SkTPin(sigma.fY, 0.0f, 135.0f);
640
641 result = cpu_blur(ctx, sigma, input, inputBounds, dstBounds);
642 }
643
644 // Return the resultOffset if the blur succeeded.
645 if (result != nullptr) {
646 *offset = resultOffset;
647 }
648 return result;
649 }
650
651 #if SK_SUPPORT_GPU
gpuFilter(const Context & ctx,SkVector sigma,const sk_sp<SkSpecialImage> & input,SkIRect inputBounds,SkIRect dstBounds,SkIPoint inputOffset,SkIPoint * offset) const652 sk_sp<SkSpecialImage> SkBlurImageFilterImpl::gpuFilter(
653 const Context& ctx, SkVector sigma, const sk_sp<SkSpecialImage> &input, SkIRect inputBounds,
654 SkIRect dstBounds, SkIPoint inputOffset, SkIPoint* offset) const {
655 if (0 == sigma.x() && 0 == sigma.y()) {
656 offset->fX = inputBounds.x() + inputOffset.fX;
657 offset->fY = inputBounds.y() + inputOffset.fY;
658 return input->makeSubset(inputBounds);
659 }
660
661 auto context = ctx.getContext();
662
663 sk_sp<GrTextureProxy> inputTexture(input->asTextureProxyRef(context));
664 if (!inputTexture) {
665 return nullptr;
666 }
667
668 // TODO (michaelludwig) - The color space choice is odd, should it just be ctx.refColorSpace()?
669 auto renderTargetContext = SkGpuBlurUtils::GaussianBlur(
670 context,
671 std::move(inputTexture),
672 SkColorTypeToGrColorType(input->colorType()),
673 input->alphaType(),
674 input->subset().topLeft(),
675 ctx.colorSpace() ? sk_ref_sp(input->getColorSpace()) : nullptr,
676 dstBounds,
677 inputBounds,
678 sigma.x(),
679 sigma.y(),
680 to_texture_domain_mode(fTileMode));
681 if (!renderTargetContext) {
682 return nullptr;
683 }
684
685 return SkSpecialImage::MakeDeferredFromGpu(
686 context,
687 SkIRect::MakeWH(dstBounds.width(), dstBounds.height()),
688 kNeedNewImageUniqueID_SpecialImage,
689 renderTargetContext->asTextureProxyRef(),
690 renderTargetContext->colorInfo().colorType(),
691 sk_ref_sp(input->getColorSpace()),
692 ctx.surfaceProps());
693 }
694 #endif
695
computeFastBounds(const SkRect & src) const696 SkRect SkBlurImageFilterImpl::computeFastBounds(const SkRect& src) const {
697 SkRect bounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src) : src;
698 bounds.outset(fSigma.width() * 3, fSigma.height() * 3);
699 return bounds;
700 }
701
onFilterNodeBounds(const SkIRect & src,const SkMatrix & ctm,MapDirection,const SkIRect * inputRect) const702 SkIRect SkBlurImageFilterImpl::onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm,
703 MapDirection, const SkIRect* inputRect) const {
704 SkVector sigma = map_sigma(fSigma, ctm);
705 return src.makeOutset(SkScalarCeilToInt(sigma.x() * 3), SkScalarCeilToInt(sigma.y() * 3));
706 }
707